R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document

INSTALL NECESSARY PACKAGES:

# install.packages("erp.easy")
library(erp.easy)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(Hmisc)
## Loading required package: lattice
## Loading required package: survival
## Loading required package: Formula
## Loading required package: ggplot2
## 
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:dplyr':
## 
##     src, summarize
## The following objects are masked from 'package:base':
## 
##     format.pval, units
library(ggplot2, quietly = TRUE, warn.conflicts = FALSE)
library(stringr)
library(tidyr)
library(reshape2)
## 
## Attaching package: 'reshape2'
## The following object is masked from 'package:tidyr':
## 
##     smiths

LOCATE FOLDERS:

# Locate the folder for the EEG output files (.txt) for old and new nets, replace the file location below with the one in your local device:
path_newnets <- "/Users/aysuerdemir/Desktop/R workspace/ERP_Zoo/CrossSectional/Mix/newnets/"
path_oldnets <- "/Users/aysuerdemir/Desktop/R workspace/ERP_Zoo/CrossSectional/Mix/oldnets/"
# Enter the number of participants in each group:
subs_new <- 70
subs_old <- 12

LOAD DATA:

# Load data into dataframes for each condition separately (the exported .txt files appear separately for each condition):
neg_go <- load.data(path_newnets,"NegGo", subs_new, -100, 999) 
neg_nogo <- load.data(path_newnets,"NegNoGo", subs_new, -100, 999)
neut_go <- load.data(path_newnets,"NeutGo", subs_new, -100, 999)
neut_nogo <- load.data(path_newnets,"NeutNoGo", subs_new, -100, 999)

# Combine all conditions together into a single dataframe:
combo_new <- rbind.data.frame(neg_go, neg_nogo, neut_go, neut_nogo) 
combo_new <- as.data.frame(unclass(combo_new), stringsAsFactors=TRUE)

# Repeat for old nets:
neg_go_old <- load.data(path_oldnets,"NegGo", subs_old, -100, 999) 
neg_nogo_old <- load.data(path_oldnets,"NegNoGo", subs_old, -100, 999)
neut_go_old <- load.data(path_oldnets,"NeutGo", subs_old, -100, 999)
neut_nogo_old <- load.data(path_oldnets,"NeutNoGo", subs_old, -100, 999)

combo_old <- rbind.data.frame(neg_go_old, neg_nogo_old, neut_go_old, neut_nogo_old) 
combo_old <- as.data.frame(unclass(combo_old),stringsAsFactors=TRUE)

head(combo_old)
##    Subject Stimulus Time        V1        V2        V3        V4        V5
## 1 AN122116    NegGo -100 -1.602166 -0.975723 -0.816704 -1.601339 -0.178200
## 2 AN122116    NegGo  -96 -1.381717 -0.857681 -0.553844 -0.892159  0.475065
## 3 AN122116    NegGo  -92 -0.916006 -0.567245 -0.245930 -0.231857  1.066748
## 4 AN122116    NegGo  -88 -0.271272 -0.088409  0.125656  0.340374  1.469912
## 5 AN122116    NegGo  -84  0.449695  0.554581  0.589043  0.840698  1.634542
## 6 AN122116    NegGo  -80  1.147286  1.300797  1.164803  1.323421  1.615967
##          V6        V7        V8        V9       V10       V11       V12
## 1 -0.331926 -0.151961 -0.347968 -0.576614 -1.093344 -1.390728 -1.446274
## 2  0.510233  0.606494 -0.153863 -0.260136 -0.415079 -0.283929 -0.484142
## 3  1.314797  1.333362  0.159202  0.034664  0.071394  0.791458  0.575123
## 4  1.898442  1.850423  0.551349  0.315926  0.373709  1.671227  1.558418
## 5  2.160961  2.056113  0.989324  0.634567  0.603586  2.271284  2.303516
## 6  2.138047  1.978029  1.457013  1.057877  0.921005  2.612692  2.723587
##        V13       V14       V15       V16      V17       V18       V19       V20
## 1 0.154436 -0.183043 -0.986050 -1.668789 0.050217 -2.640511 -3.116714 -2.074911
## 2 0.945122  0.372879 -0.317448 -0.819794 0.983765 -1.523602 -1.794190 -1.187153
## 3 1.697449  0.793301  0.149136  0.003761 1.976685 -0.314388 -0.273128 -0.085423
## 4 2.217609  1.043167  0.441242  0.719955 2.786621  0.844208  1.214652  1.044960
## 5 2.406643  1.174946  0.670528  1.307877 3.234940  1.815136  2.451749  1.997532
## 6 2.313116  1.303911  0.981770  1.808807 3.283058  2.517334  3.306489  2.624617
##         V21       V22       V23       V24       V25       V26       V27
## 1 -1.612476 -3.177330 -4.650845 -2.473480 -1.961811 -4.129242 -2.788494
## 2 -0.844498 -1.744621 -3.119498 -1.602276 -1.253998 -2.740967 -1.987232
## 3  0.098137 -0.120888 -1.237389 -0.474751 -0.290592 -1.055275 -0.902645
## 4  1.032101  1.405754  0.652441  0.778458  0.766792  0.610847  0.338024
## 5  1.764500  2.557701  2.195314  1.949837  1.698437  1.928261  1.515244
## 6  2.181705  3.180214  3.163316  2.833687  2.327084  2.689941  2.407352
##         V28      V29      V30      V31      V32       V33       V34       V35
## 1 -1.600498 0.267609 1.704092 1.655323 1.792316 -1.247337 -1.565639  0.009226
## 2 -0.895760 0.061537 1.575657 1.587364 1.779469 -0.718452 -1.027107 -0.212204
## 3  0.037254 0.093109 1.446121 1.501526 1.689861  0.020538 -0.316589 -0.194126
## 4  1.056332 0.293676 1.282846 1.354988 1.483730  0.877956  0.484066  0.008957
## 5  1.947704 0.504688 1.051548 1.120262 1.165264  1.686760  1.225574  0.244831
## 6  2.520092 0.555014 0.739815 0.802414 0.785526  2.267891  1.750835  0.334261
##        V36      V37      V38      V39       V40      V41      V42       V43
## 1 1.743858 2.122482 1.753445 0.243980 -0.360420 2.080679 2.637558  2.488483
## 2 1.622190 1.938988 1.444054 0.475021 -0.622421 1.851799 2.304211  1.770252
## 3 1.530930 1.721893 1.114063 0.813866 -0.621868 1.605459 1.966421  1.092953
## 4 1.412546 1.461838 0.791637 1.169640 -0.392212 1.336784 1.650398  0.551357
## 5 1.209280 1.153973 0.497448 1.426853 -0.084667 1.030151 1.340042  0.177834
## 6 0.894976 0.806694 0.245754 1.489670  0.100655 0.676033 0.992119 -0.057133
##         V44       V45       V46       V47       V48       V49       V50
## 1 -0.478058 -0.116197  0.471390  0.894775  1.085449 -0.986816 -0.568535
## 2 -0.332985 -0.481936 -0.240822  0.001236  0.034695 -1.011177 -1.498154
## 3 -0.127296 -0.522299 -0.512642 -0.864632 -0.893692 -0.927132 -2.277191
## 4  0.048119 -0.285610 -0.346947 -1.536613 -1.526505 -0.777245 -2.780609
## 5  0.102623  0.035172  0.028740 -1.958992 -1.841530 -0.652672 -3.026688
## 6 -0.007149  0.196474  0.274559 -2.186066 -1.952347 -0.638664 -3.137680
##         V51       V52       V53      V54      V55       V56       V57       V58
## 1  1.070000  1.878197  2.666451 1.737392 1.327618  1.859970  1.614403  2.925186
## 2 -0.160545  0.618912  1.864506 1.494488 1.545944  0.444145  0.366844  1.426971
## 3 -1.294258 -0.504265  1.024616 1.240417 1.603912 -0.640739 -0.757164 -0.111763
## 4 -2.129997 -1.287273  0.292511 0.979656 1.427685 -1.120575 -1.491501 -1.395485
## 5 -2.613663 -1.676357 -0.227856 0.723427 1.046879 -1.017914 -1.773453 -2.266602
## 6 -2.831451 -1.767654 -0.507278 0.491032 0.583645 -0.625381 -1.747795 -2.740128
##         V59       V60       V61       V62       V63       V64       V65
## 1  2.046391  1.169464  1.071705  1.248970  1.905968  1.640926  0.002188
## 2  0.602528  0.107323  0.288998  1.150200  0.342133  0.277851 -0.932522
## 3 -0.905182 -0.946693 -0.552670  0.810631 -1.086975 -1.066990 -1.984392
## 4 -2.166298 -1.769859 -1.264359  0.314499 -2.020744 -2.095554 -2.859217
## 5 -2.966969 -2.223263 -1.700798 -0.188576 -2.319824 -2.660908 -3.353342
## 6 -3.268372 -2.299583 -1.814854 -0.550926 -2.108795 -2.801708 -3.424402
##         V66       V67       V68       V69       V70       V71       V72
## 1 -0.476954  1.102089  0.719635  1.112701 -0.058893 -1.529216 -0.755066
## 2 -1.551037  0.714247  0.691925 -0.130219 -0.911140 -2.128438 -1.079086
## 3 -2.686024  0.118654  0.379486 -1.340378 -1.866013 -2.868699 -1.558258
## 4 -3.593519 -0.555941 -0.125336 -2.261200 -2.673973 -3.511125 -2.041573
## 5 -4.072910 -1.152937 -0.655533 -2.744957 -3.165016 -3.872424 -2.387306
## 6 -4.085543 -1.551479 -1.043991 -2.798599 -3.303223 -3.889922 -2.522308
##         V73       V74       V75       V76       V77       V78      V79      V80
## 1  0.441437  1.033527  0.788539 -0.928551  0.435883  1.081963 2.877953 1.601650
## 2  0.346336  0.178783  0.142629 -1.132133  0.487083  1.028404 2.960096 1.862282
## 3  0.022926 -0.656792 -0.560802 -1.508812  0.197792  0.684425 2.804230 1.968708
## 4 -0.425003 -1.350021 -1.180889 -1.935188 -0.334747  0.144081 2.413523 1.838276
## 5 -0.850845 -1.810688 -1.617779 -2.277023 -0.924843 -0.414665 1.895298 1.485500
## 6 -1.131081 -2.012646 -1.844580 -2.443001 -1.380531 -0.811607 1.405289 1.014156
##        V81       V82       V83       V84       V85      V86      V87      V88
## 1 0.848020  0.460829 -1.231041  0.076736  0.138611 2.784137 3.245194 2.589694
## 2 1.362525 -0.121952 -1.526024  0.125122  0.144287 2.785977 3.160503 2.606918
## 3 1.802489 -0.749256 -1.918602 -0.192474 -0.095490 2.586747 2.883027 2.559120
## 4 2.045585 -1.354576 -2.313644 -0.775436 -0.506226 2.155099 2.388915 2.431592
## 5 2.060138 -1.855576 -2.615715 -1.429977 -0.948303 1.556672 1.757413 2.244977
## 6 1.915174 -2.186262 -2.768425 -1.953003 -1.282578 0.921325 1.127977 2.041994
##         V89       V90       V91       V92      V93      V94       V95       V96
## 1 -3.432014 -4.049820 -0.531204  2.401985 1.829981 3.451154 -7.335152 -3.743881
## 2 -3.773735 -3.862244 -0.350449  2.178871 1.818653 3.347659 -7.251877 -3.261444
## 3 -4.165321 -3.830048 -0.507690  1.777306 1.638340 3.070870 -7.257523 -3.043457
## 4 -4.501930 -3.879913 -0.965775  1.195198 1.223476 2.590276 -7.266975 -3.139648
## 5 -4.653816 -3.891159 -1.573624  0.498116 0.631874 1.963420 -7.130493 -3.444870
## 6 -4.536186 -3.759476 -2.132874 -0.209702 0.001291 1.298814 -6.726074 -3.756775
##         V97       V98      V99      V100      V101     V102     V103     V104
## 1  1.323845  1.687925 1.786144 -4.663246 -2.786003 0.974030 0.679365 1.707811
## 2  1.275421  1.668030 1.726355 -4.882811 -2.492363 0.976803 0.704914 1.570347
## 3  0.907425  1.538475 1.584257 -5.189646 -2.378700 1.009205 0.810230 1.400173
## 4  0.181274  1.197460 1.261652 -5.422186 -2.516228 0.916962 0.854427 1.136353
## 5 -0.772461  0.651726 0.757948 -5.394495 -2.855194 0.627228 0.746681 0.772690
## 6 -1.715271 -0.001589 0.154560 -5.005291 -3.257599 0.161217 0.467112 0.346331
##       V105     V106      V107      V108      V109     V110     V111     V112
## 1 3.157804 0.831240 -0.703478 -1.796181 -0.954175 1.336458 1.670211 1.949009
## 2 3.067249 1.364780 -0.046059 -2.326027 -0.743787 1.344398 1.535836 2.193771
## 3 2.822426 1.789238  0.593539 -2.794525 -0.242855 1.456902 1.459781 2.292099
## 4 2.376984 1.999977  1.056548 -2.981224  0.310246 1.582771 1.452214 2.270863
## 5 1.775845 1.987045  1.255971 -2.768326  0.687672 1.625034 1.492644 2.191158
## 6 1.124949 1.833754  1.224303 -2.219368  0.758452 1.505865 1.527507 2.118137
##        V113      V114      V115      V116     V117     V118      V119      V120
## 1 -0.539551 -1.150284 -1.752474 -1.822952 1.795980 1.812792 -0.461781 -0.497742
## 2  0.007809 -1.588699 -1.885380 -1.500015 1.551477 1.941059 -0.014542 -0.606674
## 3  0.575939 -1.906067 -1.803888 -0.736660 1.388708 1.938408  0.414474 -0.748016
## 4  1.070267 -1.970215 -1.570978  0.139423 1.359306 1.884426  0.785519 -0.875206
## 5  1.428329 -1.725597 -1.263821  0.797108 1.457943 1.859196  1.088673 -0.915016
## 6  1.653008 -1.227242 -0.942726  1.035713 1.612221 1.904932  1.350513 -0.806335
##        V121      V122     V123      V124      V125      V126      V127
## 1 -2.506559 -1.355415 1.636124 -0.138519 -0.575409 -0.063583 -1.093063
## 2 -2.282681 -1.201950 1.676567  0.323805 -0.402016  0.300964 -0.635666
## 3 -1.849167 -0.738174 1.668022  0.681597 -0.205864  0.443298 -0.282318
## 4 -1.320885 -0.027748 1.768433  0.942221 -0.033119  0.364227 -0.125992
## 5 -0.809322  0.793083 2.099190  1.165342  0.105431  0.162506 -0.223267
## 6 -0.376049  1.563683 2.669037  1.429461  0.232117 -0.026382 -0.554886
##        V128     V129
## 1 -0.922371 0.064854
## 2 -0.654320 0.651667
## 3 -0.289268 1.148424
## 4  0.054832 1.419500
## 5  0.246422 1.427877
## 6  0.202545 1.258244

SPECIFY THE ELECTRODE NUMBERS FOR P2, N2 and P3:

FCz = frontrocentral - midline FC4 = frontrocentral - right FC3 = frontrocentral - left

Pz = parietal - midline P4 = parietal - right P3 = parietal - left

# new nets: 

FCz_newnets <- c("V18", "V16", "V10", "V19", "V11", "V4", "V12", "V5", "V6")
FC4_newnets <- c("V3","V123","V124", "V117", "V118") 
FC3_newnets <- c("V23","V27","V24",  "V28", "V20")

Pz_newnets <- c("V54","V79","V61", "V62","V78", "V67", "V72", "V77")
P4_newnets <- c("V85", "V86", "V91" , "V92" ,"V97", "V98")
P3_newnets <- c("V51", "V52","V53", "V59", "V60", "V47")

# repeat for old nets

FCz_oldnets <- c("V19","V16", "V10", "V20","V11", "V4","V12","V5","V6")
FC4_oldnets <- c("V3","V123","V119", "V123","V118")
FC3_oldnets <- c("V24","V25","V21", "V28","V29")

Pz_oldnets <- c( "V54","V62","V80", "V61","V68","V79", "V67", "V73","V78")
P4_oldnets <- c("V87","V99", "V86", "V93", "V98", "V92")
P3_oldnets <- c("V53","V48", "V52", "V60", "V51", "V59") 

CHECK OUT INDIVIDUAL WAVEFORMS:

USING MOSAIC YOU CAN CREATE AN AVERAGE WAVEFORM FOR EACH PARTICIPANT IN EACH GROUP & NET LOCATION:

#Create average waveform plots for each subject in a single, multiplot window
mosaic(combo_new, FCz_newnets, cols = 3, rows = 2)

mosaic(combo_new, Pz_newnets, cols = 3, rows = 2)

mosaic(combo_old, FCz_oldnets, cols = 3, rows = 2)

mosaic(combo_old, Pz_oldnets, cols = 3, rows = 2)

CHECK OUT GRADN AVERAGES USING GRANDAVERAGE() FUNCTION

# grand average plots the grand average waveform for each condition present in the dataframe you provide.
# A color-coded and labeled legend is generated with the plot for ease of identification of each condition.
grandaverage(combo_new, FCz_newnets)

## [[1]]
##   [1]   0.620642811   0.536617575   0.444544065   0.350994646   0.266199886
##   [6]   0.200362103   0.159344825   0.141578127   0.137688784   0.133321806
##  [11]   0.114262289   0.071871425   0.006608219  -0.071913768  -0.148551006
##  [16]  -0.208585024  -0.243585932  -0.254755410  -0.252279222  -0.250926089
##  [21]  -0.263731908  -0.296445421  -0.345153843  -0.398165552  -0.441318179
##  [26]  -0.464391487  -0.465775446  -0.453271767  -0.440621606  -0.441222268
##  [31]  -0.461885316  -0.499533657  -0.542552297  -0.576504476  -0.591979389
##  [36]  -0.591323862  -0.591313506  -0.620447854  -0.711652568  -0.893069805
##  [41]  -1.180311856  -1.572877178  -2.055664638  -2.604468375  -3.192922510
##  [46]  -3.798023925  -4.402404894  -4.993173562  -5.558820152  -6.086440603
##  [51]  -6.561104519  -6.967905411  -7.295622073  -7.539992221  -7.704640519
##  [56]  -7.798871740  -7.833190278  -7.814645657  -7.744340052  -7.618513568
##  [61]  -7.432897879  -7.188322881  -6.894842281  -6.572198583  -6.246034465
##  [66]  -5.941345208  -5.675957340  -5.456888241  -5.281102714  -5.140175057
##  [71]  -5.026550217  -4.938377302  -4.880707729  -4.862646275  -4.892063651
##  [76]  -4.970605876  -5.091498673  -5.241153616  -5.403658419  -5.565842735
##  [81]  -5.720392548  -5.865668711  -6.002776141  -6.131839437  -6.249848992
##  [86]  -6.351495602  -6.432624452  -6.494297752  -6.544852703  -6.598276286
##  [91]  -6.669143919  -6.766328910  -6.888638887  -7.024823703  -7.158454371
##  [96]  -7.275668254  -7.372218148  -7.456389224  -7.546206673  -7.662025486
## [101]  -7.817859662  -8.015500933  -8.244169902  -8.485896722  -8.723999467
## [106]  -8.950635135  -9.169843189  -9.394693083  -9.639992148  -9.914107987
## [111] -10.213812386 -10.524581756 -10.826113875 -11.100357794 -11.338289051
## [116] -11.542420787 -11.724100097 -11.897144268 -12.070977506 -12.246448410
## [121] -12.416056522 -12.568107044 -12.692575548 -12.785854414 -12.852320311
## [126] -12.902358719 -12.948091044 -12.999003687 -13.059376756 -13.128298608
## [131] -13.201722149 -13.275173059 -13.345647579 -13.411988989 -13.474004687
## [136] -13.531213200 -13.582276629 -13.625495843 -13.660041352 -13.686964744
## [141] -13.709166619 -13.729966214 -13.750733938 -13.768760095 -13.776529825
## [146] -13.763092621 -13.717269297 -13.631586714 -13.505419686 -13.346011038
## [151] -13.166797429 -12.983486394 -12.809222241 -12.650565095 -12.505760670
## [156] -12.365969744 -12.219076917 -12.054734722 -11.868827992 -11.665637757
## [161] -11.456812570 -11.257484165 -11.080897552 -10.933608713 -10.813162124
## [166] -10.709218367 -10.607712181 -10.496351821 -10.369130303 -10.227962151
## [171] -10.080790302  -9.937100341  -9.802980444  -9.678116824  -9.556167422
## [176]  -9.428305283  -9.288155473  -9.135586646  -8.977307168  -8.823835054
## [181]  -8.684220168  -8.561142267  -8.449006617  -8.336189567  -8.210597441
## [186]  -8.065901240  -7.905425392  -7.741607371  -7.591071610  -7.467489779
## [191]  -7.375527097  -7.308701313  -7.252079913  -7.188528022  -7.105538705
## [196]  -6.999535622  -6.875889127  -6.745013132  -6.616821775  -6.496372330
## [201]  -6.382716562  -6.271101640  -6.156886086  -6.038749667  -5.919273573
## [206]  -5.802640430  -5.690866329  -5.580972114  -5.464970716  -5.333017414
## [211]  -5.178267486  -5.000982925  -4.809586359  -4.617918603  -4.439903879
## [216]  -4.284211711  -4.151457589  -4.035254938  -3.926421279  -3.818064598
## [221]  -3.708877773  -3.602987486  -3.506558159  -3.423149338  -3.350462971
## [226]  -3.280342871  -3.202145506  -3.107746414  -2.995551365  -2.871336103
## [231]  -2.745427722  -2.627672197  -2.522849259  -2.428948770  -2.339182468
## [236]  -2.246637657  -2.148981594  -2.050487238  -1.960032963  -1.885852238
## [241]  -1.829675051  -1.783413081  -1.730497565  -1.651763146  -1.533491168
## [246]  -1.373989789  -1.185531351  -0.990499598  -0.813160332  -0.670513719
## [251]  -0.566123917  -0.489496494  -0.421195252  -0.341365478  -0.237943487
## [256]  -0.111140083   0.027524446   0.160845565   0.273552573   0.358575390
## [261]   0.419431648   0.468027573   0.519205959   0.584632738   0.668611063
## [266]   0.767346159   0.871537748   0.970800175   1.057822444   1.130575137
## [271]   1.191955527   1.247481986   1.302391046   1.359533219   1.418875954
## 
## [[2]]
##   [1]   0.436058541   0.376215329   0.333848603   0.307213040   0.289065279
##   [6]   0.269118489   0.238573033   0.194614795   0.142356297   0.092857041
##  [11]   0.057887695   0.043909514   0.048080227   0.058330903   0.057709537
##  [16]   0.031214276  -0.027980546  -0.115725835  -0.218640260  -0.319176194
##  [21]  -0.401614670  -0.456409930  -0.481379322  -0.479887684  -0.457787483
##  [26]  -0.421296598  -0.376911489  -0.332845621  -0.300179533  -0.291957190
##  [31]  -0.319608817  -0.387764630  -0.490030433  -0.608727135  -0.720089624
##  [36]  -0.803731017  -0.852850603  -0.880802297  -0.920670257  -1.017146084
##  [41]  -1.213117702  -1.535906887  -1.988402894  -2.548303033  -3.175340465
##  [46]  -3.823386643  -4.452754990  -5.038331048  -5.571235984  -6.054598897
##  [51]  -6.496327868  -6.902328178  -7.272552413  -7.600510368  -7.875365038
##  [56]  -8.084913546  -8.217921960  -8.265280516  -8.220708724  -8.081896457
##  [61]  -7.852356408  -7.543268321  -7.174052368  -6.770450506  -6.359785800
##  [66]  -5.964325887  -5.595028438  -5.248338608  -4.907751230  -4.550055411
##  [71]  -4.154447695  -3.711644654  -3.229828356  -2.735120854  -2.266196751
##  [76]  -1.864577495  -1.563544343  -1.378910754  -1.304451756  -1.313573608
##  [81]  -1.366979143  -1.424173184  -1.455246449  -1.449352238  -1.417007894
##  [86]  -1.385233611  -1.386971808  -1.448551003  -1.579949383  -1.771634059
##  [91]  -1.999274127  -2.234506094  -2.457436338  -2.665539067  -2.874880146
##  [96]  -3.113101917  -3.407256257  -3.771912178  -4.202744973  -4.678397995
## [101]  -5.169674113  -5.651647494  -6.112806240  -6.556916203  -6.997082916
## [106]  -7.445217311  -7.902352529  -8.354712494  -8.777716676  -9.146053117
## [111]  -9.444743044  -9.675600906  -9.855907765 -10.010021638 -10.158036265
## [116] -10.306932029 -10.448518162 -10.565006073 -10.639274954 -10.664571389
## [121] -10.648920383 -10.612472125 -10.579340163 -10.568040422 -10.585080537
## [126] -10.624604265 -10.673712830 -10.720326114 -10.759490989 -10.795265008
## [131] -10.837759860 -10.897175279 -10.977876352 -11.075370268 -11.177338790
## [136] -11.267801311 -11.332168954 -11.361213697 -11.352883221 -11.311937140
## [141] -11.248065287 -11.173333016 -11.099488540 -11.035252000 -10.983664449
## [146] -10.940262713 -10.893310316 -10.826929968 -10.726811114 -10.586696063
## [151] -10.412896805 -10.224208940 -10.046169505  -9.901175338  -9.798539095
## [156]  -9.729140905  -9.667544767  -9.581379798  -9.444363803  -9.247472470
## [161]  -9.003136027  -8.740388979  -8.493186522  -8.287210817  -8.131060348
## [166]  -8.015477884  -7.920446098  -7.826368549  -7.723753305  -7.616743963
## [171]  -7.519300630  -7.446670714  -7.406822675  -7.396181348  -7.401443662
## [176]  -7.406070432  -7.397652706  -7.372129821  -7.332820402  -7.285541054
## [181]  -7.233179232  -7.173151263  -7.099340368  -7.007434216  -6.900279495
## [186]  -6.789550375  -6.691870857  -6.620854232  -6.578978211  -6.553783478
## [191]  -6.520896086  -6.453168390  -6.332189500  -6.156798524  -5.944415762
## [196]  -5.724420921  -5.526502765  -5.369253586  -5.253865398  -5.165463752
## [201]  -5.081129684  -4.980645154  -4.854913354  -4.708422268  -4.555314030
## [206]  -4.411444006  -4.286357290  -4.178841695  -4.077764149  -3.967502841
## [211]  -3.835122568  -3.675947068  -3.495385025  -3.306642656  -3.125617913
## [216]  -2.965065781  -2.830280827  -2.717685070  -2.616458365  -2.512256868
## [221]  -2.391719533  -2.246577924  -2.076199613  -1.887805573  -1.694244249
## [226]  -1.510018406  -1.346769033  -1.209619202  -1.095801667  -0.996306644
## [231]  -0.900161773  -0.799612359  -0.693938746  -0.590137178  -0.500021997
## [236]  -0.434870016  -0.399781929  -0.390611784  -0.395138295  -0.398120644
## [241]  -0.387911173  -0.361588694  -0.326234148  -0.295596703  -0.283500505
## [246]  -0.297077040  -0.333073438  -0.378980665  -0.418315940  -0.437471735
## [251]  -0.430977887  -0.402800644  -0.363197749  -0.322819275  -0.287224760
## [256]  -0.254623500  -0.217808684  -0.169099833  -0.105687581  -0.032619543
## [261]   0.038286957   0.093226662   0.122469960   0.124618127   0.106919840
## [266]   0.081585294   0.059810730   0.046364792   0.037423619   0.022842354
## [271]  -0.008191408  -0.060906981  -0.131154203  -0.205829552  -0.267382194
## 
## [[3]]
##   [1]   0.44000211   0.35993513   0.29113869   0.24170809   0.21264901
##   [6]   0.19747459   0.18552431   0.16723828   0.13858045   0.10230704
##  [11]   0.06544650   0.03444344   0.01071746  -0.01069943  -0.03847425
##  [16]  -0.07958578  -0.13427998  -0.19440565  -0.24651410  -0.27850545
##  [21]  -0.28654922  -0.27847246  -0.27136453  -0.28406251  -0.32804125
##  [26]  -0.40124354  -0.48818566  -0.56661233  -0.61767691  -0.63469492
##  [31]  -0.62606520  -0.61083301  -0.60921339  -0.63307183  -0.68149437
##  [36]  -0.74403200  -0.81024305  -0.88072961  -0.97365716  -1.12268559
##  [41]  -1.36633329  -1.73310147  -2.22889926  -2.83245472  -3.50086093
##  [46]  -4.18285664  -4.83401028  -5.42736654  -5.95553382  -6.42443809
##  [51]  -6.84268020  -7.21211525  -7.52400504  -7.76205591  -7.91008909
##  [56]  -7.95996853  -7.91573041  -7.79229872  -7.61006574  -7.38854991
##  [61]  -7.14240891  -6.88138126  -6.61331341  -6.34767920  -6.09690451
##  [66]  -5.87438529  -5.69025717  -5.54747048  -5.44069752  -5.35902557
##  [71]  -5.29137587  -5.23188977  -5.18249751  -5.15131498  -5.14785267
##  [76]  -5.17762309  -5.23899975  -5.32388208  -5.42161748  -5.52386061
##  [81]  -5.62756126  -5.73441700  -5.84727862  -5.96577387  -6.08389384
##  [86]  -6.19122865  -6.27749881  -6.33810985  -6.37771473  -6.40965061
##  [91]  -6.45127695  -6.51744098  -6.61521839  -6.74240482  -6.89029897
##  [96]  -7.04916398  -7.21336428  -7.38347317  -7.56440747  -7.76091349
## [101]  -7.97308425  -8.19458998  -8.41470936  -8.62324465  -8.81569370
## [106]  -8.99588929  -9.17452459  -9.36421331  -9.57346509  -9.80250193
## [111] -10.04285193 -10.28075123 -10.50259013 -10.69970058 -10.87035258
## [116] -11.01837162 -11.14961804 -11.26844520 -11.37595114 -11.47067313
## [121] -11.55097023 -11.61736265 -11.67329832 -11.72383913 -11.77314921
## [126] -11.82234258 -11.86904870 -11.90900906 -11.93883844 -11.95836020
## [131] -11.97105244 -11.98222022 -11.99586285 -12.01202572 -12.02615180
## [136] -12.03094963 -12.01992370 -11.99077090 -11.94679782 -11.89549376
## [141] -11.84492494 -11.79975224 -11.75887951 -11.71590746 -11.66212524
## [146] -11.59054517 -11.49892717 -11.39027732 -11.27065541 -11.14558322
## [151] -11.01697617 -10.88222836 -10.73590728 -10.57319772 -10.39322541
## [156] -10.20048341 -10.00343728  -9.81094787  -9.62819395  -9.45407209
## [161]  -9.28140998  -9.10001842  -8.90135776  -8.68280146  -8.44961832
## [166]  -8.21382658  -7.99046174  -7.79269706  -7.62769223  -7.49462844
## [171]  -7.38550328  -7.28821012  -7.19059085  -7.08399277  -6.96529179
## [176]  -6.83690618  -6.70507739  -6.57714601  -6.45886193  -6.35256606
## [181]  -6.25670460  -6.16669258  -6.07673530  -5.98195712  -5.88007054
## [186]  -5.77191415  -5.66069248  -5.55025295  -5.44307045  -5.33880188
## [191]  -5.23409494  -5.12394408  -5.00410938  -4.87359008  -4.73599427
## [196]  -4.59905027  -4.47220562  -4.36306801  -4.27404604  -4.20070056
## [201]  -4.13265423  -4.05690312  -3.96236312  -3.84397439  -3.70471303
## [206]  -3.55464844  -3.40729356  -3.27467579  -3.16307781  -3.07107260
## [211]  -2.99056525  -2.91035479  -2.82073115  -2.71717868  -2.60168053
## [216]  -2.48120726  -2.36418382  -2.25655200  -2.15914658  -2.06745532
## [221]  -1.97382282  -1.87096520  -1.75513347  -1.62743000  -1.49278629
## [226]  -1.35719722  -1.22464459  -1.09523380  -0.96561845  -0.83150785
## [231]  -0.69098072  -0.54683605  -0.40662043  -0.28005757  -0.17487525
## [236]  -0.09289432  -0.02836163   0.03044279   0.09677594   0.18005066
## [241]   0.28188401   0.39540485   0.50830137   0.60838052   0.68916116
## [246]   0.75291389   0.80965615   0.87248220   0.95130266   1.04778634
## [251]   1.15391353   1.25494939   1.33566080   1.38701021   1.41032375
## [256]   1.41700651   1.42392013   1.44633070   1.49150121   1.55573831
## [261]   1.62611967   1.68609471   1.72241724   1.73045151   1.71573272
## [266]   1.69135008   1.67253000   1.67101066   1.69160752   1.73220640
## [271]   1.78667474   1.84887969   1.91573509   1.98779890   2.06740503
## 
## [[4]]
##   [1]   0.39975740   0.34610585   0.30585477   0.27462875   0.24665815
##   [6]   0.21522409   0.17391607   0.11879264   0.05083308  -0.02274529
##  [11]  -0.08962940  -0.13628564  -0.15334031  -0.14012060  -0.10602736
##  [16]  -0.06769487  -0.04283334  -0.04332207  -0.07074717  -0.11655280
##  [21]  -0.16668197  -0.20841091  -0.23594682  -0.25204857  -0.26509003
##  [26]  -0.28331381  -0.30958210  -0.33957628  -0.36453047  -0.37687871
##  [31]  -0.37557989  -0.36795865  -0.36683017  -0.38437738  -0.42626844
##  [36]  -0.48971565  -0.56717539  -0.65425335  -0.75771494  -0.89891985
##  [41]  -1.10979745  -1.42206293  -1.85382849  -2.39950832  -3.02798593
##  [46]  -3.69048535  -4.33530950  -4.92362484  -5.43992190  -5.89313012
##  [51]  -6.30831789  -6.71273990  -7.12211787  -7.53243649  -7.91986150
##  [56]  -8.24799647  -8.47911451  -8.58510314  -8.55474266  -8.39574124
##  [61]  -8.13196996  -7.79750719  -7.42928276  -7.05973780  -6.71070843
##  [66]  -6.38951086  -6.08814850  -5.78639449  -5.45855601  -5.08261080
##  [71]  -4.64905541  -4.16636213  -3.66072883  -3.16982247  -2.73251661
##  [76]  -2.37827852  -2.12009933  -1.95344322  -1.86109582  -1.82139180
##  [81]  -1.81614252  -1.83513586  -1.87599152  -1.94041553  -2.02952212
##  [86]  -2.14088587  -2.26876110  -2.40699951  -2.55263420  -2.70796473
##  [91]  -2.87991536  -3.07698998  -3.30545806  -3.56677999  -3.85752925
##  [96]  -4.17164938  -4.50362720  -4.85080652  -5.21366768  -5.59412169
## [101]  -5.99291681  -6.40759335  -6.83210836  -7.25817645  -7.67740535
## [106]  -8.08305151  -8.47061638  -8.83734550  -9.18134290  -9.50112186
## [111]  -9.79595938 -10.06671708 -10.31624348 -10.54854322 -10.76678344
## [116] -10.97097636 -11.15674714 -11.31617168 -11.44070860 -11.52504416
## [121] -11.57001767 -11.58308063 -11.57585912 -11.55999764 -11.54333526
## [126] -11.52827906 -11.51309302 -11.49518294 -11.47435230 -11.45396116
## [131] -11.43908990 -11.43274909 -11.43240713 -11.42923103 -11.41110244
## [136] -11.36843534 -11.30000428 -11.21562623 -11.13374156 -11.07441971
## [141] -11.05070328 -11.06228021 -11.09464200 -11.12433948 -11.12825086
## [146] -11.09272376 -11.01840287 -10.91868167 -10.81266045 -10.71596868
## [151] -10.63383596 -10.55949355 -10.47837075 -10.37586066 -10.24473593
## [156] -10.08867414  -9.92034682  -9.75528264  -9.60455631  -9.46987288
## [161]  -9.34336178  -9.21204239  -9.06476824  -8.89831413  -8.71984784
## [166]  -8.54466852  -8.39018096  -8.26867130  -8.18186058  -8.11940733
## [171]  -8.06194171  -7.98751592  -7.87906976  -7.73034187  -7.54824250
## [176]  -7.35081757  -7.16146627  -7.00133189  -6.88244779  -6.80394013
## [181]  -6.75266490  -6.70812293  -6.64994281  -6.56515049  -6.45235295
## [186]  -6.32118560  -6.18718588  -6.06423307  -5.95784719  -5.86235012
## [191]  -5.76320482  -5.64362600  -5.49236691  -5.30901737  -5.10435773
## [196]  -4.89546790  -4.69782307  -4.51813030  -4.35135276  -4.18335549
## [201]  -3.99792814  -3.78480250  -3.54493727  -3.29074356  -3.04125017
## [206]  -2.81472325  -2.62227662  -2.46530654  -2.33734638  -2.22878079
## [211]  -2.13159647  -2.04163473  -1.95754073  -1.87760447  -1.79694738
## [216]  -1.70727623  -1.59984032  -1.47007372  -1.32133967  -1.16532389
## [221]  -1.01826572  -0.89447466  -0.80005976  -0.72993368  -0.66966816
## [226]  -0.60135945  -0.51088143  -0.39330455  -0.25423717  -0.10690676
## [231]   0.03335943   0.15461733   0.25151355   0.32545371   0.38247793
## [236]   0.43041361   0.47657827   0.52649525   0.58319849   0.64652886
## [241]   0.71218337   0.77134542   0.81193522   0.82215582   0.79556177
## [246]   0.73550514   0.65646679   0.58055353   0.52970196   0.51650787
## [251]   0.53786344   0.57498410   0.60057195   0.59048933   0.53485049
## [256]   0.44330520   0.34159485   0.26059133   0.22269600   0.23188612
## [261]   0.27213357   0.31498569   0.33269545   0.31062907   0.25301954
## [266]   0.17946133   0.11411364   0.07313828   0.05671626   0.04949986
## [271]   0.02910738  -0.02175542  -0.10574330  -0.20879695  -0.30659325
grandaverage(combo_new, Pz_newnets)

## [[1]]
##   [1] -0.3099443839 -0.2266575446 -0.1361012339 -0.0489629911  0.0221364804
##   [6]  0.0672305714  0.0827642929  0.0729499071  0.0481206107  0.0209224089
##  [11]  0.0019597554 -0.0033147982  0.0044920482  0.0200133500  0.0358125268
##  [16]  0.0453403625  0.0450752821  0.0353497911  0.0199148893  0.0047505661
##  [21] -0.0034956893  0.0008888982  0.0212130179  0.0573822500  0.1055426946
##  [26]  0.1586123589  0.2077566018  0.2446064732  0.2635322286  0.2631041964
##  [31]  0.2461665911  0.2185426893  0.1870965304  0.1581556661  0.1373081036
##  [36]  0.1308742768  0.1484910321  0.2052787929  0.3220676250  0.5226898536
##  [41]  0.8286928268  1.2528611214  1.7937338911  2.4330984732  3.1376100196
##  [46]  3.8640298696  4.5664640786  5.2034231446  5.7429764018  6.1651240196
##  [51]  6.4616325625  6.6342909786  6.6927560000  6.6526110125  6.5335715304
##  [56]  6.3574370089  6.1455046018  5.9156707571  5.6798787268  5.4429639375
##  [61]  5.2036081929  4.9573814875  4.7007810679  4.4347196911  4.1659852625
##  [66]  3.9061275964  3.6681733536  3.4626162964  3.2944070679  3.1622866857
##  [71]  3.0605648339  2.9824158518  2.9230351411  2.8813020375  2.8592024125
##  [76]  2.8594843071  2.8828146786  2.9260038411  2.9822886518  3.0435659554
##  [81]  3.1035475571  3.1603777018  3.2175206232  3.2824093321  3.3635054732
##  [86]  3.4670964286  3.5953026714  3.7460285696  3.9147515036  4.0971422000
##  [91]  4.2912840071  4.4983290339  4.7213185196  4.9627337054  5.2220790536
##  [96]  5.4946040143  5.7718011036  6.0434876964  6.3006304500  6.5376458804
## [101]  6.7532459911  6.9495163964  7.1298256982  7.2965501857  7.4495814589
## [106]  7.5861817946  7.7021474036  7.7936583625  7.8588521893  7.8984999804
## [111]  7.9156014857  7.9143646750  7.8990829411  7.8735134339  7.8409436054
## [116]  7.8047954821  7.7691365500  7.7386361964  7.7177863071  7.7097698804
## [121]  7.7154566732  7.7330841946  7.7588832661  7.7885313714  7.8187910929
## [126]  7.8486554232  7.8795462286  7.9146177089  7.9575284286  8.0112182964
## [131]  8.0771711179  8.1553189732  8.2444347893  8.3425547982  8.4472019018
## [136]  8.5553750179  8.6636386321  8.7684958732  8.8671716375  8.9585311071
## [141]  9.0437065143  9.1258234643  9.2086652500  9.2946629232  9.3831685839
## [146]  9.4698706429  9.5478862143  9.6102832589  9.6531501036  9.6777814607
## [151]  9.6908425339  9.7021961554  9.7211991643  9.7529659893  9.7961811821
## [156]  9.8434516643  9.8841356893  9.9085136607  9.9114763732  9.8942458768
## [161]  9.8634807429  9.8283854911  9.7970924393  9.7739267482  9.7585956661
## [166]  9.7474966964  9.7362096107  9.7218853750  9.7043599304  9.6856592982
## [171]  9.6682216286  9.6528380589  9.6373342036  9.6167319179  9.5847664536
## [176]  9.5360527018  9.4679511786  9.3814386804  9.2806927696  9.1716483071
## [181]  9.0601477196  8.9503836464  8.8441704661  8.7410321857  8.6389091304
## [186]  8.5351069161  8.4271970286  8.3135837375  8.1937075268  8.0679780589
## [191]  7.9376546125  7.8046752946  7.6714867411  7.5408019732  7.4153283804
## [196]  7.2973176000  7.1879768036  7.0868262411  6.9913222750  6.8969681125
## [201]  6.7980768911  6.6891547429  6.5666354679  6.4303907607  6.2843001554
## [206]  6.1354274518  5.9918743786  5.8599419411  5.7415677446  5.6331392000
## [211]  5.5263439804  5.4110317857  5.2790553518  5.1276571875  4.9610621464
## [216]  4.7895740589  4.6263450964  4.4829830214  4.3655586304  4.2725393411
## [221]  4.1952358214  4.1204868893  4.0345039143  3.9266202179  3.7917600661
## [226]  3.6310877125  3.4509692536  3.2608880089  3.0710953214  2.8905087482
## [231]  2.7252298446  2.5778479018  2.4475763250  2.3310521821  2.2236316982
## [236]  2.1209017071  2.0199875786  1.9200823446  1.8219785804  1.7267094875
## [241]  1.6339851946  1.5411212232  1.4431598375  1.3343263714  1.2103739232
## [246]  1.0707233518  0.9193428750  0.7637797732  0.6127107000  0.4729911429
## [251]  0.3475032571  0.2347631536  0.1304685857  0.0301592875 -0.0683937036
## [256] -0.1642411107 -0.2541274661 -0.3346202054 -0.4043237446 -0.4649431250
## [261] -0.5206156857 -0.5758182911 -0.6331338714 -0.6920825089 -0.7497409214
## [266] -0.8027424339 -0.8496318232 -0.8922304000 -0.9352157518 -0.9839995107
## [271] -1.0421069625 -1.1094614393 -1.1825446482 -1.2562443500 -1.3263973000
## 
## [[2]]
##   [1] -0.2805428929 -0.1739377911 -0.0515514018  0.0639560839  0.1501216250
##   [6]  0.1926871911  0.1900000143  0.1520652804  0.0953621661  0.0364928250
##  [11] -0.0126200518 -0.0464592411 -0.0646095125 -0.0687950679 -0.0606091518
##  [16] -0.0414497214 -0.0142954804  0.0147523357  0.0369021500  0.0434377214
##  [21]  0.0297888911 -0.0008455839 -0.0367483661 -0.0611149036 -0.0587020429
##  [26] -0.0225428375  0.0426208518  0.1217895625  0.1954897393  0.2475448964
##  [31]  0.2714512732  0.2722809946  0.2633036446  0.2595566196  0.2721868857
##  [36]  0.3064775071  0.3644064464  0.4502945429  0.5764517161  0.7649109714
##  [41]  1.0430748446  1.4342389429  1.9469708589  2.5677871214  3.2602549375
##  [46]  3.9712925839  4.6428930161  5.2250607446  5.6853343518  6.0121133429
##  [51]  6.2120458750  6.3036813125  6.3101020446  6.2529242661  6.1491112357
##  [56]  6.0104970071  5.8445484768  5.6550398107  5.4426311036  5.2059761018
##  [61]  4.9435812571  4.6560808107  4.3481995893  4.0295786804  3.7134131732
##  [66]  3.4128386982  3.1365458304  2.8861600482  2.6569453125  2.4416831018
##  [71]  2.2360843625  2.0435100786  1.8764342321  1.7532923804  1.6916886321
##  [76]  1.7012232625  1.7793403161  1.9119965161  2.0787597161  2.2602369982
##  [81]  2.4446440018  2.6303357286  2.8232418750  3.0309428554  3.2568519375
##  [86]  3.4973637929  3.7431135607  3.9836728679  4.2133710446  4.4349459214
##  [91]  4.6587779304  4.8979234714  5.1617091696  5.4509055768  5.7566339214
##  [96]  6.0634646143  6.3555076768  6.6226293054  6.8636498268  7.0850602429
## [101]  7.2963788536  7.5046084554  7.7101486446  7.9057902643  8.0792875018
## [106]  8.2182889196  8.3151608071  8.3694888929  8.3876718321  8.3804967964
## [111]  8.3597644875  8.3351862982  8.3125351804  8.2936402054  8.2775290696
## [116]  8.2617347375  8.2433660857  8.2202287679  8.1920103518  8.1611037714
## [121]  8.1326909804  8.1141655750  8.1137179071  8.1380077893  8.1896314018
## [126]  8.2658285911  8.3593170893  8.4609237518  8.5628443643  8.6613813893
## [131]  8.7580260161  8.8580529054  8.9670094536  9.0869169143  9.2143361321
## [136]  9.3411918161  9.4579951268  9.5581316268  9.6415708107  9.7159361482
## [141]  9.7939909821  9.8883246196 10.0056964625 10.1433866946 10.2888970643
## [146] 10.4232450857 10.5270338268 10.5869870982 10.6002732089 10.5749090268
## [151] 10.5265067946 10.4728972946 10.4284960964 10.4003551643 10.3875038286
## [156] 10.3837352536 10.3821760661 10.3793574500 10.3771551250 10.3820417304
## [161] 10.4018370000 10.4412701786 10.4985467179 10.5650164393 10.6281968196
## [166] 10.6768246125 10.7057867214 10.7190208786 10.7287474482 10.7508312839
## [171] 10.7979586268 10.8738582321 10.9712019161 11.0740036393 11.1635655125
## [176] 11.2257497214 11.2564989661 11.2628868089 11.2590791464 11.2592404804
## [181] 11.2709366750 11.2918735196 11.3113060018 11.3155537679 11.2951547268
## [186] 11.2499469446 11.1893459536 11.1276753750 11.0771458179 11.0418658500
## [191] 11.0156887268 10.9850458554 10.9358112661 10.8607479321 10.7635649643
## [196] 10.6574119982 10.5587453786 10.4794926821 10.4211228536 10.3734565071
## [201] 10.3191293018 10.2417592179 10.1336148357  9.9989735679  9.8517887643
## [206]  9.7090331929  9.5827667214  9.4745578518  9.3750440446  9.2689973964
## [211]  9.1431600196  8.9927952304  8.8239279089  8.6506277250  8.4886774750
## [216]  8.3484631446  8.2304006625  8.1251897339  8.0183308214  7.8962655161
## [221]  7.7511491411  7.5825802929  7.3960598661  7.1992843179  6.9984102446
## [226]  6.7964875036  6.5946299500  6.3945373554  6.2002799089  6.0181615179
## [231]  5.8547086018  5.7136299375  5.5934391321  5.4874722929  5.3868760964
## [236]  5.2851001357  5.1813521304  5.0811515964  4.9937651464  4.9274332321
## [241]  4.8844266125  4.8584052821  4.8359013696  4.8014150893  4.7436528679
## [246]  4.6600258393  4.5576545214  4.4504668839  4.3533828429  4.2760248750
## [251]  4.2189053929  4.1736509196  4.1266874821  4.0644168714  3.9778481107
## [256]  3.8651225196  3.7310488321  3.5841230893  3.4328054321  3.2830010446
## [261]  3.1373539429  2.9959957661  2.8579851357  2.7228049125  2.5910490821
## [266]  2.4639263893  2.3421063250  2.2250563000  2.1113036750  1.9993440000
## [271]  1.8886176857  1.7803709786  1.6778430143  1.5852979411  1.5060724321
## 
## [[3]]
##   [1] -0.032319182 -0.008137191  0.012102234  0.026555957  0.033891579
##   [6]  0.034094757  0.028349939  0.018242961  0.004876932 -0.011522088
##  [11] -0.031238329 -0.054006864 -0.077880645 -0.098840243 -0.111672966
##  [16] -0.111798620 -0.097144580 -0.069135659 -0.032290841  0.007548704
##  [21]  0.045637484  0.080183657  0.112623889  0.146143991  0.183316361
##  [26]  0.224041504  0.264817071  0.299736405  0.322808000  0.330462189
##  [31]  0.322906986  0.303711804  0.278198050  0.251864554  0.230053916
##  [36]  0.219355905  0.230310466  0.279880120  0.391702832  0.592871155
##  [41]  0.907571721  1.349544075  1.915985552  2.585246955  3.319464725
##  [46]  4.071454463  4.793490189  5.444983077  5.996874525  6.432274682
##  [51]  6.744348884  6.933312977  7.004287625  6.966911652  6.836106030
##  [56]  6.632393314  6.380319234  6.104781489  5.826342677  5.557354127
##  [61]  5.300726832  5.052264468  4.805804389  4.558880634  4.316288932
##  [66]  4.089912470  3.894896859  3.743845359  3.641537489  3.582600045
##  [71]  3.553230411  3.536144439  3.516482621  3.486234193  3.445697937
##  [76]  3.401795220  3.364340818  3.342159812  3.340807521  3.362458589
##  [81]  3.407344164  3.475387209  3.567023755  3.682819329  3.822182604
##  [86]  3.982148157  4.157265191  4.341016062  4.528161466  4.716834175
##  [91]  4.909349934  5.111341823  5.329427320  5.568255225  5.828166250
##  [96]  6.104437066  6.388251493  6.668768075  6.935443470  7.179940918
## [101]  7.397154846  7.585237846  7.744917396  7.878627650  7.989757705
## [106]  8.081970698  8.158551093  8.221896357  8.273283389  8.312873507
## [111]  8.339958575  8.353445164  8.352465986  8.336843470  8.307193602
## [116]  8.264853682  8.211889279  8.151257609  8.086953934  8.023955209
## [121]  7.967808621  7.923731659  7.895314720  7.883369500  7.885704412
## [126]  7.898121139  7.916335334  7.938030952  7.964216964  7.999110670
## [131]  8.048353104  8.116208518  8.203139350  8.304994666  8.414247118
## [136]  8.522750654  8.624880471  8.719601671  8.810365613  8.902731636
## [141]  9.000873002  9.104718954  9.209091582  9.305253595  9.384247988
## [146]  9.440644411  9.474926666  9.493356770  9.505427864  9.520136066
## [151]  9.542715111  9.573053566  9.606313573  9.635354925  9.653759139
## [156]  9.658049309  9.648259120  9.626991464  9.597710746  9.563106613
## [161]  9.524174912  9.480365612  9.430571184  9.374285759  9.312302396
## [166]  9.246823093  9.181129941  9.118962016  9.063717607  9.017634116
## [171]  8.981134405  8.952371041  8.927082784  8.898998929  8.861080848
## [176]  8.807424104  8.735159354  8.645575338  8.544002293  8.438282402
## [181]  8.336194896  8.242714186  8.158345352  8.079250339  7.999006762
## [186]  7.911148189  7.811462196  7.699110943  7.576173461  7.445928916
## [191]  7.310934207  7.172037534  7.028731141  6.880491116  6.728300332
## [196]  6.575487130  6.427243254  6.288858209  6.163518255  6.050888286
## [201]  5.947228614  5.846978420  5.745044209  5.638825752  5.529025154
## [206]  5.418820839  5.311813375  5.209955591  5.112528596  5.016576429
## [211]  4.918454052  4.815698384  4.708216523  4.598110466  4.488101850
## [216]  4.379478687  4.270786907  4.157977775  4.036009768  3.901192166
## [221]  3.753213220  3.595752489  3.435153943  3.277675748  3.126652212
## [226]  2.980934523  2.835321239  2.682928884  2.518660371  2.342306334
## [231]  2.159792773  1.981846400  1.820643795  1.685713216  1.580571461
## [236]  1.501339068  1.437923834  1.377343723  1.307879570  1.222425007
## [241]  1.119999321  1.005130941  0.885487202  0.768728868  0.659844854
## [246]  0.560004791  0.467141593  0.377725739  0.288814177  0.199616413
## [251]  0.111895305  0.029037850 -0.045736895 -0.111053196 -0.168129250
## [256] -0.220333029 -0.271748311 -0.325232459 -0.380811134 -0.435240748
## [261] -0.483056014 -0.518649495 -0.538492273 -0.542672980 -0.535178979
## [266] -0.522718571 -0.512524280 -0.510126714 -0.518115566 -0.536315211
## [271] -0.563113225 -0.597230443 -0.639044670 -0.690749373 -0.755031932
## 
## [[4]]
##   [1] -0.1350278875 -0.1247200500 -0.1336267214 -0.1571642661 -0.1829112679
##   [6] -0.1940614321 -0.1758355482 -0.1221050321 -0.0393350875  0.0541166679
##  [11]  0.1337322143  0.1778116518  0.1759301732  0.1329262339  0.0667108571
##  [16]  0.0011070571 -0.0430903357 -0.0551926839 -0.0369270036  0.0008484304
##  [21]  0.0449951964  0.0866309161  0.1240000732  0.1608049429  0.2015021107
##  [26]  0.2463547179  0.2890897500  0.3189020946  0.3259907411  0.3074511196
##  [31]  0.2699810071  0.2279785018  0.1981357750  0.1933600500  0.2192461714
##  [36]  0.2753702714  0.3611973661  0.4833389732  0.6596071589  0.9168725464
##  [41]  1.2828267250  1.7746741268  2.3892775679  3.0992423393  3.8572541429
##  [46]  4.6072600750  5.2978142964  5.8923661036  6.3732954607  6.7393064768
##  [51]  6.9983713946  7.1599975054  7.2305838875  7.2134506929  7.1122073304
##  [56]  6.9344097536  6.6930879643  6.4053239000  6.0886450482  5.7570866161
##  [61]  5.4192323393  5.0794730679  4.7415066714  4.4115833536  4.0994827196
##  [66]  3.8167251375  3.5728779161  3.3717057982  3.2092519250  3.0752859018
##  [71]  2.9575909036  2.8468556964  2.7398475054  2.6398864589  2.5549216357
##  [76]  2.4942925500  2.4657056286  2.4738613536  2.5209700446  2.6079789286
##  [81]  2.7349677339  2.9004321786  3.1000417018  3.3259403286  3.5675148286
##  [86]  3.8137888911  4.0570830732  4.2961541696  4.5367873893  4.7893411214
##  [91]  5.0644094732  5.3684645304  5.7011907589  6.0555707643  6.4207028161
##  [96]  6.7859218268  7.1438368821  7.4908562929  7.8255216768  8.1460262786
## [101]  8.4483427089  8.7258566839  8.9708242732  9.1768207000  9.3404659321
## [106]  9.4612332339  9.5397181321  9.5759180679  9.5688866696  9.5181936375
## [111]  9.4268204607  9.3041481482  9.1669635464  9.0368912304  8.9347338018
## [116]  8.8741444500  8.8573566964  8.8748045786  8.9089640893  8.9411605964
## [121]  8.9586171464  8.9585650893  8.9477312589  8.9381363714  8.9415676821
## [126]  8.9651705500  9.0098183357  9.0717443054  9.1462794411  9.2312160982
## [131]  9.3276504875  9.4381026036  9.5633119893  9.6995395536  9.8378342804
## [136]  9.9660610696 10.0731750589 10.1537157571 10.2101323000 10.2518201607
## [141] 10.2914808321 10.3404321089 10.4045317696 10.4823287911 10.5662773679
## [146] 10.6463564232 10.7141717286 10.7657447339 10.8022507321 10.8288288375
## [151] 10.8520823893 10.8771943982 10.9060007500 10.9368475429 10.9658657821
## [156] 10.9887769268 11.0025580982 11.0066900357 11.0035258643 10.9973869339
## [161] 10.9929696196 10.9938705054 11.0016877286 11.0156694054 11.0330365321
## [166] 11.0501502750 11.0640718429 11.0736491589 11.0795767571 11.0837102089
## [171] 11.0879899714 11.0932177946 11.0981754857 11.0997685482 11.0943729107
## [176] 11.0795749804 11.0552889964 11.0238597696 10.9891856482 10.9550044821
## [181] 10.9228612089 10.8908805429 10.8542466018 10.8072239000 10.7456470304
## [186] 10.6689394982 10.5809686643 10.4892086196 10.4021422839 10.3258803893
## [191] 10.2616498839 10.2052125036 10.1482815214 10.0813418464  9.9971224071
## [196]  9.8934490482  9.7740786804  9.6470268250  9.5212863446  9.4033358714
## [201]  9.2945570768  9.1903537661  9.0814561714  8.9571353714  8.8088948661
## [206]  8.6331528339  8.4322611518  8.2139614000  7.9896843679  7.7720164661
## [211]  7.5722011714  7.3983398036  7.2542825482  7.1387695036  7.0448681304
## [216]  6.9605243357  6.8706333768  6.7604821446  6.6199478839  6.4474254714
## [221]  6.2519040643  6.0515004286  5.8680752250  5.7196979071  5.6138409107
## [226]  5.5440193446  5.4915163732  5.4321398607  5.3457903482  5.2246937679
## [231]  5.0764241964  4.9202915125  4.7787022768  4.6671926500  4.5872456107
## [236]  4.5253607393  4.4591061875  4.3674199946  4.2401269679  4.0822984589
## [241]  3.9117026339  3.7506783500  3.6160487661  3.5118087696  3.4282026196
## [246]  3.3475819179  3.2538085607  3.1404290357  3.0139689732  2.8910607321
## [251]  2.7907558893  2.7256465500  2.6961331536  2.6905463661  2.6905670161
## [256]  2.6788066839  2.6451250036  2.5892844857  2.5192508196  2.4463310571
## [261]  2.3799767339  2.3248111071  2.2806125464  2.2440805268  2.2107361107
## [266]  2.1759616232  2.1350695554  2.0829235464  2.0142458786  1.9254640143
## [271]  1.8175424125  1.6977432607  1.5785866554  1.4738786625  1.3934069339
# butterfly plots all individual waveforms for the condition specified by the stim argument(i.e.,a butterfly plot).
# The grandaverage waveform is also plotted,using a red line.
butterfly(combo_new,FCz_newnets, stim=1)

CODE BELOW GETS ALL THE MEASURES (N2, P2, P3) FROM OLD AND NEW NET DATA, COMBINE THEM TOGETHER AND IT SAVES THE DATA INTO A FINAL COMBO SPREADSHEET:

CHECK THE WINDOW RANGE FOR EACH ERP COMPONENT AND ADJUST AS NEEDED!

P2 between 180–280 ms after stimulus onset at frontal, frontocentral and central electrode sites.

N2 between 320,520 ms after stimulus onset at frontal, frontocentral and central electrode sites.

P3 between 450-750 ms after stimulus onset at parietal electrode sites.

# Get the mean Amplitude measures from the NEW net:
MeanAmp_P2_FCz_newnets <- (m.measures(combo_new, FCz_newnets, window=c(180,280)))

MeanAmp_P2_FC4_newnets <- (m.measures(combo_new, FC4_newnets, window=c(180,280)))

MeanAmp_P2_FC3_newnets <- (m.measures(combo_new, FC3_newnets, window=c(180,280))) 

MeanAmp_N2_FCz_newnets <- (m.measures(combo_new, FCz_newnets, window=c(320,520)))  

MeanAmp_N2_FC4_newnets <- (m.measures(combo_new, FC4_newnets, window=c(320,520)))

MeanAmp_N2_FC3_newnets<- (m.measures(combo_new, FC3_newnets, window=c(320,520)))  

MeanAmp_P3_Pz_newnets <- (m.measures(combo_new, Pz_newnets, window=c(450,750)))  

MeanAmp_P3_P4_newnets <- (m.measures(combo_new, P4_newnets, window=c(450,750)))  

MeanAmp_P3_P3_newnets <- (m.measures(combo_new, P3_newnets, window=c(450,750)))  

# We need to combine these but each one of these datasets use the same variable name - Mean Amplitude. 
# Below is a function that will allow us to rename the variables in multiple datasets in a similar way:

rename_datasets_amplitude <- function(dataset_list, new_col_names){
for (i in 1:length(dataset_list)){
assign(dataset_list[i], rename(get(dataset_list[i]),
!!new_col_names[i] := "Mean Amplitude"), envir = .GlobalEnv)
}
}

datasets <- c("MeanAmp_P2_FCz_newnets", "MeanAmp_P2_FC4_newnets", "MeanAmp_P2_FC3_newnets",
              "MeanAmp_N2_FCz_newnets", "MeanAmp_N2_FC4_newnets", "MeanAmp_N2_FC3_newnets",
              "MeanAmp_P3_Pz_newnets", "MeanAmp_P3_P4_newnets", "MeanAmp_P3_P3_newnets")

new_column_names <- c("MeanAmp_P2_FCz", "MeanAmp_P2_FC4", "MeanAmp_P2_FC3",
                      "MeanAmp_N2_FCz", "MeanAmp_N2_FC4", "MeanAmp_N2_FC3",
                      "MeanAmp_P3_Pz", "MeanAmp_P3_P4", "MeanAmp_P3_P3")

rename_datasets_amplitude(datasets, new_column_names)

#  load multiple datasets into the workspace     
datasets_list <- mget(datasets)

# Using the `Reduce()` function to merge multiple data frames stored in `datasets_list` into a single data frame called `df_merge1`. 
# It does this by merging each data frame in the list with the others, based on the columns "Subject" and "Trial Type".


merge_datasets_amplitude <- function(datasets_list) {
Reduce(function(x, y) {
x <- x[, !(names(x) %in% "Standard Dev")] # ignore Standard Dev column from the first dataframe, do not merge it
y <- y[, !(names(y) %in% "Standard Dev")] # ignore Standard Dev column from the next dataframe, do not merge it
merge(x, y, by=c("Subject", "Trial Type"))
}, datasets_list)
}

# Run the function
df_merge1 <- merge_datasets_amplitude(datasets_list) # use the function to merge the new data frames

# Get the Latency measures from the NEW net:
Latency_P2_FCz_newnets <- (p.measures(combo_new, FCz_newnets, window=c(180,280), pol="pos"))

Latency_P2_FC4_newnets <- (p.measures(combo_new, FC4_newnets, window=c(180,280), pol="pos"))

Latency_P2_FC3_newnets <- (p.measures(combo_new, FC3_newnets, window=c(180,280), pol="pos"))

Latency_N2_FCz_newnets <- (p.measures(combo_new, FCz_newnets, window=c(320,520), pol="neg"))  

Latency_N2_FC4_newnets <- (p.measures(combo_new, FC4_newnets, window=c(320,520), pol="neg"))  

Latency_N2_FC3_newnets <- (p.measures(combo_new, FC3_newnets, window=c(320,520), pol="neg")) 

Latency_P3_Pz_newnets <- (p.measures(combo_new, Pz_newnets, window=c(450,750), pol="pos"))

Latency_P3_P4_newnets <- (p.measures(combo_new, P4_newnets, window=c(450,750), pol="pos"))

Latency_P3_P3_newnets <- (p.measures(combo_new, P3_newnets, window=c(450,750), pol="pos"))

# Function `rename_datasets()` that renames the columns "Peak Latency" and "Peak Amplitude" in each data frame from a list of data frames (`dataset_list`)

rename_datasets_latency <- function(dataset_list, new_col_name1, new_col_name2){
for (i in 1:length(dataset_list)){
temp_data <- get(dataset_list[i])
names(temp_data)[names(temp_data) == "Peak Latency"] <- new_col_name1[i]
names(temp_data)[names(temp_data) == "Peak Amplitude"] <- new_col_name2[i]
assign(dataset_list[i], temp_data, envir = .GlobalEnv)
}
}

datasets <- c("Latency_P2_FCz_newnets", "Latency_P2_FC4_newnets", "Latency_P2_FC3_newnets",
              "Latency_N2_FCz_newnets", "Latency_N2_FC4_newnets", "Latency_N2_FC3_newnets",
              "Latency_P3_Pz_newnets", "Latency_P3_P4_newnets", "Latency_P3_P3_newnets")

new_column_names1 <- c("Latency_P2_FCz", "Latency_P2_FC4", "Latency_P2_FC3",
                      "Latency_N2_FCz", "Latency_N2_FC4", "Latency_N2_FC3",
                      "Latency_P3_Pz", "Latency_P3_P4", "Latency_P3_P3")

new_column_names2 <- c("PeakAmp_P2_FCz", "PeakAmp_P2_FC4", "PeakAmp_P2_FC3",
                      "PeakAmp_N2_FCz", "PeakAmp_N2_FC4", "PeakAmp_N2_FC3",
                      "PeakAmp_P3_Pz", "PeakAmp_P3_P4", "PeakAmp_P3_P3")

rename_datasets_latency(datasets, new_column_names1, new_column_names2)

datasets_list <- mget(datasets)


merge_datasets_latency <- function(datasets_list) {
Reduce(function(x, y) {
merge(x, y, by=c("Subject", "Trial Type"))
}, datasets_list)
}

# Run the function
df_merge2 <- merge_datasets_latency(datasets_list) # use the function to merge the new data frames

# Combine the 2 dataframes
ERP_newnets <- full_join(df_merge1, df_merge2)
## Joining with `by = join_by(Subject, `Trial Type`)`
head(ERP_newnets)
##    Subject Trial Type MeanAmp_P2_FCz MeanAmp_P2_FC4 MeanAmp_P2_FC3
## 1 AE050318      NegGo     -8.6567894     -10.997870      -8.274136
## 2 AE050318    NegNoGo      0.1368089       3.073859     -10.233068
## 3 AE050318     NeutGo    -11.0096655     -10.983267      -8.061495
## 4 AE050318   NeutNoGo     -8.0131387      -7.947222      -8.404834
## 5 AH101121      NegGo     -6.6852204      -9.178219      -7.760717
## 6 AH101121    NegNoGo     -2.2374154      -4.678083      -4.003878
##   MeanAmp_N2_FCz MeanAmp_N2_FC4 MeanAmp_N2_FC3 MeanAmp_P3_Pz MeanAmp_P3_P4
## 1     -17.640501    -17.0612959      -12.45291      4.369997    -1.0128204
## 2      -2.763603     -0.6563204      -11.78551      3.345500     0.4492580
## 3     -18.544452    -17.9227477      -12.32419      4.253425    -0.6663896
## 4     -14.488076    -16.2244929      -12.51434      6.818025    -1.6119402
## 5     -22.625932    -24.2391735      -23.10149     18.547604    11.2572051
## 6     -22.243306    -20.8193210      -24.12986     14.173249    11.9982319
##   MeanAmp_P3_P3 Latency_P2_FCz PeakAmp_P2_FCz Latency_P2_FC4 PeakAmp_P2_FC4
## 1      7.006061            212     -6.2425933            208      -8.224361
## 2      2.221935            252      3.3000709            232       5.512485
## 3      6.126728            220     -8.4652572            220      -9.281374
## 4     12.549254            216     -4.2941628            216      -3.940464
## 5     16.239133            228     -5.6526816            188      -7.083663
## 6      5.765768            240     -0.5854749            204      -1.564159
##   Latency_P2_FC3 PeakAmp_P2_FC3 Latency_N2_FCz PeakAmp_N2_FCz Latency_N2_FC4
## 1            200      -6.982536            352     -23.058663            352
## 2            188      -8.637317            452      -5.257907            400
## 3            212      -6.498847            384     -22.615500            384
## 4            180      -6.557909            320     -20.985998            320
## 5            236      -6.305076            492     -25.080314            484
## 6            240      -1.848016            396     -24.552156            384
##   PeakAmp_N2_FC4 Latency_N2_FC3 PeakAmp_N2_FC3 Latency_P3_Pz PeakAmp_P3_Pz
## 1     -20.626624            368      -17.04337           612      6.037603
## 2      -3.018455            408      -12.41742           600      6.301873
## 3     -21.637845            388      -14.78690           592      6.610696
## 4     -21.846657            324      -16.75670           640     10.395567
## 5     -27.616219            520      -26.08764           528     23.899982
## 6     -27.499941            472      -26.48135           508     18.808306
##   Latency_P3_P4 PeakAmp_P3_P4 Latency_P3_P3 PeakAmp_P3_P3
## 1           576     0.8441270           512      9.862223
## 2           508     3.1160960           648      5.847591
## 3           488     0.3001207           516      8.529244
## 4           508     0.4807528           524     14.750284
## 5           536    15.0651712           512     20.561269
## 6           584    16.8901785           652      4.791160
# remove standard dev column:
# ERP_newnets <- ERP_newnets %>% select(-`Standard Dev`)

REPEAT FOR OLD NETS!

MeanAmp_P2_FCz_oldnets <- (m.measures(combo_old, FCz_oldnets, window=c(180,280)))

MeanAmp_P2_FC4_oldnets <- (m.measures(combo_old, FC4_oldnets, window=c(180,280)))

MeanAmp_P2_FC3_oldnets <- (m.measures(combo_old, FC3_oldnets, window=c(180,280)))

MeanAmp_N2_FCz_oldnets <- (m.measures(combo_old, FCz_oldnets, window=c(320,520)))  

MeanAmp_N2_FC4_oldnets <- (m.measures(combo_old, FC4_oldnets, window=c(320,520)))  

MeanAmp_N2_FC3_oldnets <- (m.measures(combo_old, FC3_oldnets, window=c(320,520)))  

MeanAmp_P3_Pz_oldnets <- (m.measures(combo_old, Pz_oldnets, window=c(450,750)))  

MeanAmp_P3_P4_oldnets <- (m.measures(combo_old, P4_oldnets, window=c(450,750)))  

MeanAmp_P3_P3_oldnets <- (m.measures(combo_old, P3_oldnets, window=c(450,750)))  

datasets <- c("MeanAmp_P2_FCz_oldnets", "MeanAmp_P2_FC4_oldnets", "MeanAmp_P2_FC3_oldnets",
              "MeanAmp_N2_FCz_oldnets", "MeanAmp_N2_FC4_oldnets", "MeanAmp_N2_FC3_oldnets",
              "MeanAmp_P3_Pz_oldnets", "MeanAmp_P3_P4_oldnets", "MeanAmp_P3_P3_oldnets")

new_column_names <- c("MeanAmp_P2_FCz", "MeanAmp_P2_FC4", "MeanAmp_P2_FC3",
                      "MeanAmp_N2_FCz", "MeanAmp_N2_FC4", "MeanAmp_N2_FC3",
                      "MeanAmp_P3_Pz", "MeanAmp_P3_P4", "MeanAmp_P3_P3")

rename_datasets_amplitude(datasets, new_column_names)
                
datasets_list <- mget(datasets)

df_merge1_old <- merge_datasets_amplitude(datasets_list) # use the function to merge the new data frames



Latency_P2_FCz_oldnets <- (p.measures(combo_old, FCz_oldnets, window=c(180,280), pol="pos"))

Latency_P2_FC4_oldnets <- (p.measures(combo_old, FC4_oldnets, window=c(180,280), pol="pos"))

Latency_P2_FC3_oldnets <- (p.measures(combo_old, FC3_oldnets, window=c(180,280), pol="pos"))

Latency_N2_FCz_oldnets <- (p.measures(combo_old, FCz_oldnets, window=c(320,520), pol="neg"))  

Latency_N2_FC4_oldnets <- (p.measures(combo_old, FC4_oldnets, window=c(320,520), pol="neg"))  

Latency_N2_FC3_oldnets <- (p.measures(combo_old, FC3_oldnets, window=c(320,520), pol="neg")) 

Latency_P3_Pz_oldnets <- (p.measures(combo_old, Pz_oldnets, window=c(450,750), pol="pos"))

Latency_P3_P4_oldnets <- (p.measures(combo_old, P4_oldnets, window=c(450,750), pol="pos"))

Latency_P3_P3_oldnets <- (p.measures(combo_old, P3_oldnets, window=c(450,750), pol="pos"))

datasets <- c("Latency_P2_FCz_oldnets", "Latency_P2_FC4_oldnets", "Latency_P2_FC3_oldnets",
              "Latency_N2_FCz_oldnets", "Latency_N2_FC4_oldnets", "Latency_N2_FC3_oldnets",
              "Latency_P3_Pz_oldnets", "Latency_P3_P4_oldnets", "Latency_P3_P3_oldnets")

new_column_names1 <- c("Latency_P2_FCz", "Latency_P2_FC4", "Latency_P2_FC3",
                      "Latency_N2_FCz", "Latency_N2_FC4", "Latency_N2_FC3",
                      "Latency_P3_Pz", "Latency_P3_P4", "Latency_P3_P3")

new_column_names2 <- c("PeakAmp_P2_FCz", "PeakAmp_P2_FC4", "PeakAmp_P2_FC3",
                      "PeakAmp_N2_FCz", "PeakAmp_N2_FC4", "PeakAmp_N2_FC3",
                      "PeakAmp_P3_Pz", "PeakAmp_P3_P4", "PeakAmp_P3_P3")

rename_datasets_latency(datasets, new_column_names1, new_column_names2)

datasets_list <- mget(datasets)

df_merge2_old <- merge_datasets_latency(datasets_list) # use the function to merge the new data frames

                
# Combine the 2 dataframes
ERP_oldnets <- full_join(df_merge1_old, df_merge2_old)
## Joining with `by = join_by(Subject, `Trial Type`)`
# Combine old +new
ERP <- full_join(ERP_oldnets, ERP_newnets)
## Joining with `by = join_by(Subject, `Trial Type`, MeanAmp_P2_FCz,
## MeanAmp_P2_FC4, MeanAmp_P2_FC3, MeanAmp_N2_FCz, MeanAmp_N2_FC4, MeanAmp_N2_FC3,
## MeanAmp_P3_Pz, MeanAmp_P3_P4, MeanAmp_P3_P3, Latency_P2_FCz, PeakAmp_P2_FCz,
## Latency_P2_FC4, PeakAmp_P2_FC4, Latency_P2_FC3, PeakAmp_P2_FC3, Latency_N2_FCz,
## PeakAmp_N2_FCz, Latency_N2_FC4, PeakAmp_N2_FC4, Latency_N2_FC3, PeakAmp_N2_FC3,
## Latency_P3_Pz, PeakAmp_P3_Pz, Latency_P3_P4, PeakAmp_P3_P4, Latency_P3_P3,
## PeakAmp_P3_P3)`
# Remove Grand Ave from data, order by subject name and reset the index:
ERP <- ERP[!(ERP$Subject=="Grand Avg"),]
ERP <- with(ERP,  ERP[order(Subject) , ])
rownames(ERP) <- NULL # Reset index

MORE RE-FORMATTING:

# CREATE A NEW COLUMN by taking the difference between N2-P2
ERP$MeanAmp_N2P2_FCz <- ERP$MeanAmp_N2_FCz - ERP$MeanAmp_P2_FCz
ERP$PeakAmp_N2P2_FCz <- ERP$PeakAmp_N2_FCz - ERP$PeakAmp_P2_FCz

ERP$MeanAmp_N2P2_FC4 <- ERP$MeanAmp_N2_FC4 - ERP$MeanAmp_P2_FC4
ERP$PeakAmp_N2P2_FC4 <- ERP$PeakAmp_N2_FC4 - ERP$PeakAmp_P2_FC4

ERP$MeanAmp_N2P2_FC3 <- ERP$MeanAmp_N2_FC3 - ERP$MeanAmp_P2_FC3
ERP$PeakAmp_N2P2_FC3 <- ERP$PeakAmp_N2_FC3 - ERP$PeakAmp_P2_FC3

# REORGANIZE STIMTAG VARIABLE AS TWO SEPERATE VARIABLES: EMOTION AND CONDITION - EACH WITH TWO LEVELS 
ERP <- ERP %>%
mutate(Emotion = str_extract(`Trial Type`, "Neut|Neg"),
       Condition = str_extract(`Trial Type`, "Go|NoGo"))

# RESHAPE TO LONG FORMAT

ERP_long <- pivot_longer(ERP, 
  cols = -c(Subject, `Trial Type`, `Condition`, `Emotion`), 
  names_to = c(".value",  "Electrode"),
  names_pattern = "^(MeanAmp_P2|MeanAmp_N2|MeanAmp_N2P2|MeanAmp_P3|PeakAmp_P2|PeakAmp_N2|PeakAmp_N2P2|PeakAmp_P3|Latency_P2|Latency_N2|Latency_P3)_(.+)$",
  values_to = "Value"
) %>%
  mutate(
    Region = ifelse(grepl("^FC", Electrode), "Frontocentral",  "Parietal"),
    Laterality = ifelse(grepl("4", Electrode), "Right", 
               ifelse(grepl("3", Electrode), "Left", "Midline"))
  ) %>%
  select(-Electrode)


ERP_long <- pivot_longer(ERP, 
  cols = -c(Subject, `Trial Type`, `Condition`, `Emotion`), 
  names_to = c(".value",  "Electrode"),
  names_pattern = "^(MeanAmp_P2|MeanAmp_N2|MeanAmp_N2P2|MeanAmp_P3|PeakAmp_P2|PeakAmp_N2|PeakAmp_N2P2|PeakAmp_P3|Latency_P2|Latency_N2|Latency_P3)_(.+)$",
  values_to = "Value"
) %>%
  mutate(
    Laterality = ifelse(grepl("4", Electrode), "Right", 
               ifelse(grepl("3", Electrode), "Left", "Midline"))
  ) %>%
  select(-Electrode)


ERP_long <- ERP_long %>%
  group_by(Subject, `Trial Type`, Emotion, Condition, Laterality) %>%
  summarise(across(everything(), ~mean(., na.rm = TRUE)), .groups = 'drop')


# Write to a csv file:
write.csv(ERP_long, "/Users/aysuerdemir/Desktop/R workspace/ERP_Zoo/CrossSectional/Mix/ERP_long")
head(ERP_long)
## # A tibble: 6 × 16
##   Subject  `Trial Type` Emotion Condition Laterality MeanAmp_P2 MeanAmp_N2
##   <chr>    <chr>        <chr>   <chr>     <chr>           <dbl>      <dbl>
## 1 AE050318 NegGo        Neg     Go        Left           -8.27     -12.5  
## 2 AE050318 NegGo        Neg     Go        Midline        -8.66     -17.6  
## 3 AE050318 NegGo        Neg     Go        Right         -11.0      -17.1  
## 4 AE050318 NegNoGo      Neg     NoGo      Left          -10.2      -11.8  
## 5 AE050318 NegNoGo      Neg     NoGo      Midline         0.137     -2.76 
## 6 AE050318 NegNoGo      Neg     NoGo      Right           3.07      -0.656
## # ℹ 9 more variables: MeanAmp_P3 <dbl>, Latency_P2 <dbl>, PeakAmp_P2 <dbl>,
## #   Latency_N2 <dbl>, PeakAmp_N2 <dbl>, Latency_P3 <dbl>, PeakAmp_P3 <dbl>,
## #   MeanAmp_N2P2 <dbl>, PeakAmp_N2P2 <dbl>

LOAD AND MERGE WITH INTAKE INFO:

# Load DataSet: 
intake <- read.csv(file = '/Users/aysuerdemir/Desktop/R workspace/ERP_Zoo/CognitiveEmotionalLi-IntakeDiagnosticData_DATA_2023-10-25_0818.csv')

# Subject IDs include the visit number in the combo dataset if it is NOT the first time point. 
# Do the same here: Combine visit number with subject and create a new Subject variable so that it matches the combo:
intake <- intake  %>%
  mutate(Subject = ifelse(redcap_event_name !="t1_arm_1", paste0(part_id_status, "T", visitnumber), part_id_status)) 

# Create a new variable representing final sldper100words ("disfluency_sldper100words_final) by taking disfluency_sldper100words from CVD as primary, 
# but in the case that this data is missing, take the disfluency scores from CVE:
intake <- intake  %>%
  mutate(disfluency_sldper100words_final = ifelse(!is.na(disfluency_sldper100words), disfluency_sldper100words, disfluency_sldper100words_cve)) 

# Create a final talker group variable ("talkergroup_final) using disfluency_sldper100words_final and talker group based on parent report:
# 1: CWS, 0:CWNS, 9:unidentified
intake <- intake  %>%
  mutate(talkergroup_final = ifelse((disfluency_sldper100words_final >= 3 | calculator_talkergroup_parent == 1), 1,
                                          ifelse((disfluency_sldper100words_final < 3 & calculator_talkergroup_parent == 0), 0, 9)))  
                    
# Take the relevant columns from intake dataset
# You may update this to take more columns into the dataset:
intake <-  subset(intake, select=c('Subject','calculator_age_cve','calculator_gender_cve','race', 'ethnicity',
                                   'calculator_talkergroup_parent','tso_calculated',
                                   'disfluency_sldper100words','ssi_total', 
                                   'disfluency_sldper100words_final', 'talkergroup_final',
                                   "gfta_standard", "ppvt_standard", "evt_standard",             
                                   'teld_rec_standard','teld_exp_standard', "teld_spokenlang_standard",
                                   'tocs_1_total', 'tocs_2_total', 'tcs_total',
                                   'eprime_condorder_zootask','cve_comments','comments_tasks','handedness_zoo'))

# Merge with the main dataset using SUBJECT
FULL <- merge(ERP_long, intake, by=c("Subject"), all.x = TRUE)
head(FULL)  
##    Subject Trial Type Emotion Condition Laterality  MeanAmp_P2  MeanAmp_N2
## 1 AE050318      NegGo     Neg        Go       Left  -8.2741357 -12.4529149
## 2 AE050318      NegGo     Neg        Go    Midline  -8.6567894 -17.6405009
## 3 AE050318      NegGo     Neg        Go      Right -10.9978696 -17.0612959
## 4 AE050318    NegNoGo     Neg      NoGo       Left -10.2330683 -11.7855128
## 5 AE050318    NegNoGo     Neg      NoGo    Midline   0.1368089  -2.7636031
## 6 AE050318    NegNoGo     Neg      NoGo      Right   3.0738593  -0.6563204
##   MeanAmp_P3 Latency_P2 PeakAmp_P2 Latency_N2 PeakAmp_N2 Latency_P3 PeakAmp_P3
## 1   7.006061        200  -6.982536        368 -17.043367        512   9.862223
## 2   4.369997        212  -6.242593        352 -23.058663        612   6.037603
## 3  -1.012820        208  -8.224361        352 -20.626624        576   0.844127
## 4   2.221935        188  -8.637317        408 -12.417423        648   5.847591
## 5   3.345500        252   3.300071        452  -5.257907        600   6.301873
## 6   0.449258        232   5.512485        400  -3.018455        508   3.116096
##   MeanAmp_N2P2 PeakAmp_N2P2 calculator_age_cve calculator_gender_cve race
## 1    -4.178779   -10.060831               38.1                     0    2
## 2    -8.983711   -16.816070               38.1                     0    2
## 3    -6.063426   -12.402263               38.1                     0    2
## 4    -1.552445    -3.780106               38.1                     0    2
## 5    -2.900412    -8.557978               38.1                     0    2
## 6    -3.730180    -8.530940               38.1                     0    2
##   ethnicity calculator_talkergroup_parent tso_calculated
## 1         0                             1            1.9
## 2         0                             1            1.9
## 3         0                             1            1.9
## 4         0                             1            1.9
## 5         0                             1            1.9
## 6         0                             1            1.9
##   disfluency_sldper100words ssi_total disfluency_sldper100words_final
## 1                        12        23                              12
## 2                        12        23                              12
## 3                        12        23                              12
## 4                        12        23                              12
## 5                        12        23                              12
## 6                        12        23                              12
##   talkergroup_final gfta_standard ppvt_standard evt_standard teld_rec_standard
## 1                 1           121           126          123               146
## 2                 1           121           126          123               146
## 3                 1           121           126          123               146
## 4                 1           121           126          123               146
## 5                 1           121           126          123               146
## 6                 1           121           126          123               146
##   teld_exp_standard teld_spokenlang_standard tocs_1_total tocs_2_total
## 1               135                      149           22            6
## 2               135                      149           22            6
## 3               135                      149           22            6
## 4               135                      149           22            6
## 5               135                      149           22            6
## 6               135                      149           22            6
##   tcs_total eprime_condorder_zootask cve_comments comments_tasks handedness_zoo
## 1        25                        1                                         NA
## 2        25                        1                                         NA
## 3        25                        1                                         NA
## 4        25                        1                                         NA
## 5        25                        1                                         NA
## 6        25                        1                                         NA
# Print the unique subject codes
unique_codes<- unique(FULL$Subject)
unique_codes <- as.data.frame(unique_codes)
unique_codes
##    unique_codes
## 1      AE050318
## 2      AH101121
## 3      AK022218
## 4      AK102221
## 5      AL041819
## 6      AN122116
## 7      AS110816
## 8      AT051818
## 9      AW040217
## 10     AW110418
## 11   BW071018T2
## 12     CC031323
## 13   CC102318T2
## 14     CF101019
## 15     CL040218
## 16     CM101322
## 17     CM120919
## 18     DJ052417
## 19     EC041817
## 20     EG030618
## 21     EM100417
## 22     ES031519
## 23     ES032018
## 24     FM032823
## 25     FW121816
## 26     GB012717
## 27   GR091721T2
## 28     HC102117
## 29     HC111621
## 30     HH061919
## 31     HW110822
## 32     JA092118
## 33   JG091119T3
## 34     JJ011018
## 35   JK032119T3
## 36     JM041823
## 37     JS121321
## 38     JT051618
## 39     KA022017
## 40     KE050718
## 41     KT072319
## 42     LB012619
## 43     LB111121
## 44     LG100721
## 45     LO042723
## 46     LT112916
## 47   LW102219T3
## 48     MF101019
## 49     MM040119
## 50   MR091118T2
## 51     MS102319
## 52     NL041119
## 53     OB032423
## 54     OG013016
## 55     OJ032223
## 56     PB021519
## 57     PB031723
## 58     PW030417
## 59     PW041023
## 60     RB041423
## 61     RB101619
## 62     RC102022
## 63     RH100218
## 64   RK040219T3
## 65     RO042723
## 66     RS030518
## 67     RT032219
## 68     RT042523
## 69     SB111121
## 70     SC051023
## 71     SK041519
## 72     SL090418
## 73     SP010219
## 74   ST100121T2
## 75     TA051917
## 76     TE062818
## 77     TS011518
## 78     WB110221
## 79     WF080417
## 80     WH022219
## 81     WK011122
## 82     WS051018
# Making sure the Final FULL dataset has the all subjects coded (initially specified by SUBS - subject number):
subs = 82
(nrow(FULL)/12) == subs # This should give TRUE! 12 rows per subject
## [1] TRUE

Show the rows where talkergroup_final = 9 or NA :

undefined_talkergroup <- subset(FULL, talkergroup_final == 9 | is.na(talkergroup_final)) print(unique(undefined_talkergroup$Subject))

FIND THE UNDEFINED (9) TALKER GROUPS AND MANUALLY MARK THEM AS EITHER 1 or 0 if needed:

Replace NA values in a specific column based on a condition:

FULL\(talkergroup_final <- ifelse(FULL\)Subject == “JA092118”, 0, FULL\(talkergroup_final) FULL\)talkergroup_final <- ifelse(FULL\(Subject == "LG100721T2", 1, FULL\)talkergroup_final) FULL\(talkergroup_final <- ifelse(FULL\)Subject == “LW102219T3”, 1, FULL$talkergroup_final)

Making sure no 9 or NA remained:

any(FULL\(talkergroup_final == 9 | is.na(FULL\)talkergroup_final))

# How many kids in each group?
talkergroup_counts <- table(FULL$talkergroup_final)
print(talkergroup_counts)/12
## 
##   0   1 
## 504 480
## 
##  0  1 
## 42 40

ALSO ADD THE AVAILABLE LONGITUDINAL CATEGORIES:

Load DataSet:

longitudinal_groups <- read.csv(file = ‘/Users/aysuerdemir/Desktop/R workspace/ERP_Zoo/LongitudinalClassifications_9.10.23.csv’)

Merge with the main dataset using SUBJECT

FULL <- merge(FULL, longitudinal_groups[, c(“Subject”, “Longitudinal_Group_0Rec_1Per_2Und”)], by=c(“Subject”), all.x = TRUE)

Making sure the Final FULL dataset has the all subjects coded (initially specified by SUBS - subject number):

(nrow(FULL)/4) == subs # This should give TRUE!

How many kids in each group?

longtgroup_counts <- table(FULL$Longitudinal_Group_0Rec_1Per_2Und) print(longtgroup_counts)/4

LOAD AND MERGE WITH BEHAVIORAL DATA:

Trials with responses faster than 200 ms were eliminated from the analysis, as they were too quick to reflect responding to the current stimulus.

Go proportion correct 0.75 0.185 0.07–1.0 )1.03 0.76 Go RT (ms) 934 155.5 378–1,300 0.03 )0.29 No-go proportion correct 0.74 0.312 0–1 )1.16 0.02 Sensitivity (d¢) 1.54 1.114 )1.27–3.77 )0.44 )0.45

Mean reaction times in ms for CWS and CWNS. Group Hits False alarms M SD M SD CWS 509 132 382* 111 CWNS 534 104 457* 145

For each participant, the frequency of the following variables was automatically recorded and stored: (a) ‘hits’ (when a Go-stimulus was followed by a response falling between 200 and 2300 ms after stimulus onset), (b) ‘misses’ (when a Gostimulus was not followed by a response), (c) ‘false alarms’ (when a NoGo-stimulus was followed by pressing the response button between 200 and 2300 ms after stimulus onset), and (d) ‘premature responses’ (when the response button was pressed between 0 and 200 ms after stimulus onset). In addition, for the variables ‘hits’ and ‘false alarms’ mean RTs were also recorded.

In case a child exhibited a false alarm or premature response on two or more trials out of the 48 trials, this was defined as exhibiting ‘multiple false alarms’ or ‘multiple premature responses’. In addition, for the variables ‘hits’ and ‘false alarms’ mean RTs were also recorded.

# Load the file:
accuracy <- read.csv(file = '/Users/aysuerdemir/Desktop/R workspace/ERP_Zoo/Merged_Zoo_10.03.23.csv')
# Take only the relevant variables:
accuracy <-  subset(accuracy, select=c('Name','VisitNumber','ShowStim.ACC','ShowStim.RESP','ShowStim.RT','StimTag'))

# Convert all empty strings to NA
accuracy <- replace(accuracy, accuracy == "", NA)
# accuracy$ShowStim.RESP <- na_if(accuracy$ShowStim.RESP, "")

# Combine visit number with subject and create a new Subject variable for eprime so that it matches the FULL
accuracy <- accuracy  %>% 
  mutate(Subject = ifelse(VisitNumber != 1, paste0(Name, "T", VisitNumber), Name)) 

# Print unique subjects in FULL
unique_subjects <- unique(FULL$Subject)

# Filter 'accuracy' dataframe
accuracy <- accuracy[accuracy$`Subject` %in% unique_subjects, ]

# Unique counts for ShowStim.RESP
unique_codes <- unique(accuracy$ShowStim.RESP)
counts <- as.data.frame(table(accuracy$ShowStim.RESP))
counts
##        Var1  Freq
## 1     {ALT}     1
## 2 {CONTROL}     8
## 3   {SHIFT}     1
## 4   {SPACE}     3
## 5         1     5
## 6         2     2
## 7         3    53
## 8         4 20225
## 9         6    11
# Check out the class types for each variable. 
sapply(accuracy, class)
##          Name   VisitNumber  ShowStim.ACC ShowStim.RESP   ShowStim.RT 
##   "character"     "integer"     "integer"   "character"     "integer" 
##       StimTag       Subject 
##   "character"   "character"
# For ShowStim.RESP response 4 is a "character", not integer. 
print(class(accuracy$ShowStim.RESP))
## [1] "character"
# Convert character 4 for ShowStim.RESP to integer
# accuracy$ShowStim.RESP <- as.integer(accuracy$ShowStim.RESP)


# For each participant, the frequency of the following variables was automatically recorded and stored: 
# (a) ‘hits’ (when a Go-stimulus was followed by a response falling between 200 and 2300 ms after stimulus onset), 
# (b) ‘misses’ (when a Go stimulus was not followed by a response), 
# (c) ‘false alarms’ (when a NoGo-stimulus was followed by pressing the response button between 200 and 2300 ms after stimulus onset), and 
# (d) ‘premature responses’ (when the response button was pressed between 0 and 200 ms after stimulus onset). 

# Remove the special character trials, and only keep those trials with proper number resonses.
unique(accuracy$ShowStim.RESP)
##  [1] "4"         NA          "1"         "{SPACE}"   "3"         "{CONTROL}"
##  [7] "{SHIFT}"   "2"         "{ALT}"     "6"
accuracy <- accuracy[is.na(accuracy$ShowStim.RESP) | accuracy$ShowStim.RESP %in% c(1,2,3,4,5,6) , ]

# Drop the 2 rows with NA:
accuracy <- accuracy[!is.na(accuracy$StimTag), ]

# CREATE THE NEW VARIABLES:
# Our max RT is 2023

accuracy <- accuracy %>%
  mutate(behavior = case_when(
    (ShowStim.RT >= 200 & ShowStim.RESP == 4) & (StimTag %in% c("negG", "neuG")) ~ "correct_go",
    (ShowStim.RT == 0) & (StimTag %in% c("negG", "neuG")) ~ "wrong_go",
    (ShowStim.RT < 200 & ShowStim.RT > 0 & ShowStim.RESP %in% 1:6) & (StimTag %in% c("negG", "neuG")) ~ "premature_go",
    (ShowStim.RT == 0 & (StimTag %in% c("negN", "neuN"))) ~ "correct_nogo",
    (ShowStim.RT > 200 & ShowStim.RESP %in% 1:6) & (StimTag %in% c("negN", "neuN")) ~ "wrong_nogo_falsealarm",
    (ShowStim.RT < 200 & ShowStim.RT > 0 & ShowStim.RESP %in% 1:6) & (StimTag %in% c("negN", "neuN")) ~ "premature_nogo",
    TRUE ~ NA_character_   # is a catch-all condition that assigns `NA` to any rows that don't meet any of the previous conditions
  ))

# Calculate the proportions
accuracy_proportions <- accuracy %>%
  
  group_by(Subject, StimTag) %>%
  summarise(
  correct_go = sum(behavior == "correct_go", na.rm = TRUE),
  wrong_go = sum(behavior == "wrong_go", na.rm = TRUE),
  premature_go = sum(behavior == "premature_go", na.rm = TRUE),
  accuracy_go_proportion = (correct_go / (correct_go + wrong_go +premature_go)) *100 ,
  
  correct_nogo = sum(behavior == "correct_nogo", na.rm = TRUE),
  wrong_nogo_falsealarm = sum(behavior == "wrong_nogo_falsealarm", na.rm = TRUE),
  premature_nogo = sum(behavior == "premature_nogo", na.rm = TRUE),
  accuracy_nogo_proportion = (correct_nogo / (correct_nogo + wrong_nogo_falsealarm +premature_nogo))*100,
  
  premature_go = sum(behavior == "premature_go", na.rm = TRUE),
  premature_go_proportion = (premature_go / (correct_go + premature_go))*100,
  
  premature_nogo = sum(behavior == "premature_nogo", na.rm = TRUE),
  premature_nogo_proportion = (premature_nogo / (wrong_nogo_falsealarm + premature_nogo))*100,
  
RT_proper_go = mean(ifelse(ShowStim.RT >= 200 & (StimTag == 'neuG' | StimTag == 'negG'), ShowStim.RT, NA), na.rm = TRUE),
RT_all_go = mean(ifelse((StimTag == 'neuG' | StimTag == 'negG') & ShowStim.RT != 0, ShowStim.RT, NA), na.rm = TRUE),
RT_nogo_falsealarm = mean(ifelse((behavior == 'wrong_nogo_falsealarm'), ShowStim.RT, NA), na.rm = TRUE)
)
## `summarise()` has grouped output by 'Subject'. You can override using the
## `.groups` argument.
# Combine accuracy go and accuracy nogo together,  premature go and nogo together, and RT go and RT nogo together:
# Since 'accuracy_go' and 'accuracy_nogo' do not overlap (one of them is always NA for a given row), you can use the `coalesce` function to pick the non-NA value

accuracy_proportions_final <- accuracy_proportions %>%
  mutate(
    accuracy = coalesce(accuracy_go_proportion, accuracy_nogo_proportion),
    premature_responses = coalesce(premature_go_proportion, premature_nogo_proportion),
    RT_proper = coalesce(RT_proper_go, RT_nogo_falsealarm))

# Subset to main variables only
accuracy_proportions_final <-  subset(accuracy_proportions_final, select=c('Subject', "StimTag", "accuracy", "premature_responses", "RT_proper", "RT_all_go"))

# Rename the labels for StimTags on eprime data
eprime <- accuracy_proportions_final %>% 
  mutate(StimTag = recode(StimTag, "negG" = "NegGo", "negN" = "NegNoGo", "neuG" = "NeutGo", "neuN" = "NeutNoGo"))

head(eprime)
## # A tibble: 6 × 6
## # Groups:   Subject [2]
##   Subject  StimTag  accuracy premature_responses RT_proper RT_all_go
##   <chr>    <chr>       <dbl>               <dbl>     <dbl>     <dbl>
## 1 AE050318 NegGo        75.8               9.90       774.      706.
## 2 AE050318 NegNoGo      57.5              23.5        566       NaN 
## 3 AE050318 NeutGo       90                 0.917      802.      796.
## 4 AE050318 NeutNoGo     70                16.7        721       NaN 
## 5 AH101121 NegGo        99.2               0          643.      643.
## 6 AH101121 NegNoGo      87.5               0          411.      NaN

COMBINE FULL WITH EPRIME

# Replace Trial Type in FULL with "StimTag" to be able to merge with eprime data
FULL <- FULL %>%
  dplyr::rename("StimTag" = "Trial Type")

# COMBINE ALL!!
ZOO <- merge(FULL, eprime, by=c("Subject", "StimTag"), all.x = TRUE)
head(ZOO)
##    Subject StimTag Emotion Condition Laterality  MeanAmp_P2  MeanAmp_N2
## 1 AE050318   NegGo     Neg        Go       Left  -8.2741357 -12.4529149
## 2 AE050318   NegGo     Neg        Go    Midline  -8.6567894 -17.6405009
## 3 AE050318   NegGo     Neg        Go      Right -10.9978696 -17.0612959
## 4 AE050318 NegNoGo     Neg      NoGo      Right   3.0738593  -0.6563204
## 5 AE050318 NegNoGo     Neg      NoGo    Midline   0.1368089  -2.7636031
## 6 AE050318 NegNoGo     Neg      NoGo       Left -10.2330683 -11.7855128
##   MeanAmp_P3 Latency_P2 PeakAmp_P2 Latency_N2 PeakAmp_N2 Latency_P3 PeakAmp_P3
## 1   7.006061        200  -6.982536        368 -17.043367        512   9.862223
## 2   4.369997        212  -6.242593        352 -23.058663        612   6.037603
## 3  -1.012820        208  -8.224361        352 -20.626624        576   0.844127
## 4   0.449258        232   5.512485        400  -3.018455        508   3.116096
## 5   3.345500        252   3.300071        452  -5.257907        600   6.301873
## 6   2.221935        188  -8.637317        408 -12.417423        648   5.847591
##   MeanAmp_N2P2 PeakAmp_N2P2 calculator_age_cve calculator_gender_cve race
## 1    -4.178779   -10.060831               38.1                     0    2
## 2    -8.983711   -16.816070               38.1                     0    2
## 3    -6.063426   -12.402263               38.1                     0    2
## 4    -3.730180    -8.530940               38.1                     0    2
## 5    -2.900412    -8.557978               38.1                     0    2
## 6    -1.552445    -3.780106               38.1                     0    2
##   ethnicity calculator_talkergroup_parent tso_calculated
## 1         0                             1            1.9
## 2         0                             1            1.9
## 3         0                             1            1.9
## 4         0                             1            1.9
## 5         0                             1            1.9
## 6         0                             1            1.9
##   disfluency_sldper100words ssi_total disfluency_sldper100words_final
## 1                        12        23                              12
## 2                        12        23                              12
## 3                        12        23                              12
## 4                        12        23                              12
## 5                        12        23                              12
## 6                        12        23                              12
##   talkergroup_final gfta_standard ppvt_standard evt_standard teld_rec_standard
## 1                 1           121           126          123               146
## 2                 1           121           126          123               146
## 3                 1           121           126          123               146
## 4                 1           121           126          123               146
## 5                 1           121           126          123               146
## 6                 1           121           126          123               146
##   teld_exp_standard teld_spokenlang_standard tocs_1_total tocs_2_total
## 1               135                      149           22            6
## 2               135                      149           22            6
## 3               135                      149           22            6
## 4               135                      149           22            6
## 5               135                      149           22            6
## 6               135                      149           22            6
##   tcs_total eprime_condorder_zootask cve_comments comments_tasks handedness_zoo
## 1        25                        1                                         NA
## 2        25                        1                                         NA
## 3        25                        1                                         NA
## 4        25                        1                                         NA
## 5        25                        1                                         NA
## 6        25                        1                                         NA
##   accuracy premature_responses RT_proper RT_all_go
## 1 75.83333             9.90099   774.044  706.0297
## 2 75.83333             9.90099   774.044  706.0297
## 3 75.83333             9.90099   774.044  706.0297
## 4 57.50000            23.52941   566.000       NaN
## 5 57.50000            23.52941   566.000       NaN
## 6 57.50000            23.52941   566.000       NaN

See how many kids have missing accuracy data and manually add them:

ADD THIS KID MANUALLY _ ACCURACY TAKEN FROM NETSTATION:

# See how many kids have missing accuracy data and manually add them:

subset_NA_eprime_data <- subset(ZOO, is.na(ZOO$accuracy))
unique(subset_NA_eprime_data$Subject)
## [1] "RB101619"
# Identify the specific row
rows_to_update_NeutNoGo <- which(ZOO$Subject == "RB101619" & ZOO$StimTag == "NeutNoGo")
rows_to_update_NeutGo <- which(ZOO$Subject == "RB101619" & ZOO$StimTag == "NeutGo")
rows_to_update_NegNoGo <- which(ZOO$Subject == "RB101619" & ZOO$StimTag == "NegNoGo")
rows_to_update_NegGo <- which(ZOO$Subject == "RB101619" & ZOO$StimTag == "NegGo")

# Replace the accuracy value
ZOO$accuracy[rows_to_update_NeutNoGo] <- 50
ZOO$accuracy[rows_to_update_NeutGo] <- 99
ZOO$accuracy[rows_to_update_NegNoGo] <- 42.5
ZOO$accuracy[rows_to_update_NegGo] <- 99

ZOO[ZOO$Subject == "RB101619",]
##      Subject  StimTag Emotion Condition Laterality MeanAmp_P2 MeanAmp_N2
## 721 RB101619    NegGo     Neg        Go    Midline  -5.390026 -17.650182
## 722 RB101619    NegGo     Neg        Go      Right -10.797848 -21.432989
## 723 RB101619    NegGo     Neg        Go       Left  -3.950041 -14.275685
## 724 RB101619  NegNoGo     Neg      NoGo       Left  -6.000802 -14.086998
## 725 RB101619  NegNoGo     Neg      NoGo      Right  -5.192217 -13.254804
## 726 RB101619  NegNoGo     Neg      NoGo    Midline  -4.264043 -14.067967
## 727 RB101619   NeutGo    Neut        Go       Left  -2.396887  -9.280133
## 728 RB101619   NeutGo    Neut        Go    Midline  -1.797148 -10.750121
## 729 RB101619   NeutGo    Neut        Go      Right  -6.212510 -14.489684
## 730 RB101619 NeutNoGo    Neut      NoGo       Left  -2.975609  -8.827129
## 731 RB101619 NeutNoGo    Neut      NoGo    Midline  -5.478988 -12.531785
## 732 RB101619 NeutNoGo    Neut      NoGo      Right  -9.838859 -21.048845
##     MeanAmp_P3 Latency_P2 PeakAmp_P2 Latency_N2 PeakAmp_N2 Latency_P3
## 721   4.185884        216 -4.1729526        404  -20.73456        540
## 722   4.828953        224 -9.1936062        388  -24.14483        516
## 723   9.714811        216 -2.9772724        404  -16.83459        564
## 724   8.974658        224 -1.9849400        340  -19.90115        592
## 725  21.664147        236 -2.8018056        332  -16.99843        728
## 726   5.752708        216  0.8061220        340  -20.08442        588
## 727   5.265058        204 -0.0317794        496  -11.15279        456
## 728   4.319342        204  1.3632187        388  -12.88830        456
## 729   4.932128        204 -3.5492104        384  -16.30434        508
## 730   7.797420        216  0.8018334        368  -12.21802        488
## 731   8.611435        208 -1.0327817        416  -14.68038        452
## 732   5.549722        200 -4.5049230        400  -23.30060        700
##     PeakAmp_P3 MeanAmp_N2P2 PeakAmp_N2P2 calculator_age_cve
## 721   6.372755   -12.260156    -16.56161               53.5
## 722   9.144339   -10.635141    -14.95122               53.5
## 723  11.680410   -10.325644    -13.85732               53.5
## 724  14.403730    -8.086196    -17.91621               53.5
## 725  29.863190    -8.062587    -14.19663               53.5
## 726  10.463715    -9.803924    -20.89054               53.5
## 727   8.270745    -6.883246    -11.12101               53.5
## 728   6.482269    -8.952973    -14.25151               53.5
## 729   9.959636    -8.277174    -12.75513               53.5
## 730  12.555105    -5.851520    -13.01985               53.5
## 731  14.208231    -7.052797    -13.64760               53.5
## 732  11.736709   -11.209987    -18.79568               53.5
##     calculator_gender_cve race ethnicity calculator_talkergroup_parent
## 721                     1    5         0                             1
## 722                     1    5         0                             1
## 723                     1    5         0                             1
## 724                     1    5         0                             1
## 725                     1    5         0                             1
## 726                     1    5         0                             1
## 727                     1    5         0                             1
## 728                     1    5         0                             1
## 729                     1    5         0                             1
## 730                     1    5         0                             1
## 731                     1    5         0                             1
## 732                     1    5         0                             1
##     tso_calculated disfluency_sldper100words ssi_total
## 721           27.5                      9.33        28
## 722           27.5                      9.33        28
## 723           27.5                      9.33        28
## 724           27.5                      9.33        28
## 725           27.5                      9.33        28
## 726           27.5                      9.33        28
## 727           27.5                      9.33        28
## 728           27.5                      9.33        28
## 729           27.5                      9.33        28
## 730           27.5                      9.33        28
## 731           27.5                      9.33        28
## 732           27.5                      9.33        28
##     disfluency_sldper100words_final talkergroup_final gfta_standard
## 721                            9.33                 1           119
## 722                            9.33                 1           119
## 723                            9.33                 1           119
## 724                            9.33                 1           119
## 725                            9.33                 1           119
## 726                            9.33                 1           119
## 727                            9.33                 1           119
## 728                            9.33                 1           119
## 729                            9.33                 1           119
## 730                            9.33                 1           119
## 731                            9.33                 1           119
## 732                            9.33                 1           119
##     ppvt_standard evt_standard teld_rec_standard teld_exp_standard
## 721           120          117               125               107
## 722           120          117               125               107
## 723           120          117               125               107
## 724           120          117               125               107
## 725           120          117               125               107
## 726           120          117               125               107
## 727           120          117               125               107
## 728           120          117               125               107
## 729           120          117               125               107
## 730           120          117               125               107
## 731           120          117               125               107
## 732           120          117               125               107
##     teld_spokenlang_standard tocs_1_total tocs_2_total tcs_total
## 721                      119           17           14        24
## 722                      119           17           14        24
## 723                      119           17           14        24
## 724                      119           17           14        24
## 725                      119           17           14        24
## 726                      119           17           14        24
## 727                      119           17           14        24
## 728                      119           17           14        24
## 729                      119           17           14        24
## 730                      119           17           14        24
## 731                      119           17           14        24
## 732                      119           17           14        24
##     eprime_condorder_zootask cve_comments
## 721                        1             
## 722                        1             
## 723                        1             
## 724                        1             
## 725                        1             
## 726                        1             
## 727                        1             
## 728                        1             
## 729                        1             
## 730                        1             
## 731                        1             
## 732                        1             
##                                                                                                                                                                                                            comments_tasks
## 721 Some talking but overall compliant child. During the Zoo Task he had a hard time not catching the monkeys despite understanding the rule very well. For example, he kept saying "my bad" after catching the monkeys. 
## 722 Some talking but overall compliant child. During the Zoo Task he had a hard time not catching the monkeys despite understanding the rule very well. For example, he kept saying "my bad" after catching the monkeys. 
## 723 Some talking but overall compliant child. During the Zoo Task he had a hard time not catching the monkeys despite understanding the rule very well. For example, he kept saying "my bad" after catching the monkeys. 
## 724 Some talking but overall compliant child. During the Zoo Task he had a hard time not catching the monkeys despite understanding the rule very well. For example, he kept saying "my bad" after catching the monkeys. 
## 725 Some talking but overall compliant child. During the Zoo Task he had a hard time not catching the monkeys despite understanding the rule very well. For example, he kept saying "my bad" after catching the monkeys. 
## 726 Some talking but overall compliant child. During the Zoo Task he had a hard time not catching the monkeys despite understanding the rule very well. For example, he kept saying "my bad" after catching the monkeys. 
## 727 Some talking but overall compliant child. During the Zoo Task he had a hard time not catching the monkeys despite understanding the rule very well. For example, he kept saying "my bad" after catching the monkeys. 
## 728 Some talking but overall compliant child. During the Zoo Task he had a hard time not catching the monkeys despite understanding the rule very well. For example, he kept saying "my bad" after catching the monkeys. 
## 729 Some talking but overall compliant child. During the Zoo Task he had a hard time not catching the monkeys despite understanding the rule very well. For example, he kept saying "my bad" after catching the monkeys. 
## 730 Some talking but overall compliant child. During the Zoo Task he had a hard time not catching the monkeys despite understanding the rule very well. For example, he kept saying "my bad" after catching the monkeys. 
## 731 Some talking but overall compliant child. During the Zoo Task he had a hard time not catching the monkeys despite understanding the rule very well. For example, he kept saying "my bad" after catching the monkeys. 
## 732 Some talking but overall compliant child. During the Zoo Task he had a hard time not catching the monkeys despite understanding the rule very well. For example, he kept saying "my bad" after catching the monkeys. 
##     handedness_zoo accuracy premature_responses RT_proper RT_all_go
## 721              1     99.0                  NA        NA        NA
## 722              1     99.0                  NA        NA        NA
## 723              1     99.0                  NA        NA        NA
## 724              1     42.5                  NA        NA        NA
## 725              1     42.5                  NA        NA        NA
## 726              1     42.5                  NA        NA        NA
## 727              1     99.0                  NA        NA        NA
## 728              1     99.0                  NA        NA        NA
## 729              1     99.0                  NA        NA        NA
## 730              1     50.0                  NA        NA        NA
## 731              1     50.0                  NA        NA        NA
## 732              1     50.0                  NA        NA        NA

LOAD and merge with trail numbers DF

# Load the file:
trialnum <- read.csv(file = '/Users/aysuerdemir/Desktop/R workspace/ERP_Zoo/Zoo_trialnumber_used_10.03.23.csv')

# convert the dataset to long format:
# id.vars argument specifies which columns in the original data frame should remain as they are without being transformed.
# measure.vars argument specifies which columns in the original data frame should be melted
# The variable.name argument specifies the name of the new column that will store the variable names from the measure.vars
# The value.name argument specifies the name of the new column that will store the values from the measure.vars.
trialnum_long <- melt(trialnum, id.vars = c("Subject"),
                     measure.vars = c("NeutNoGo","NeutGo",
                                      "NegNoGo", "NegGo"), 
                     variable.name = "StimTag", 
                     value.name ="TrialNum")

ZOO <- merge(ZOO, trialnum_long, by=c("Subject", "StimTag"), all.x = TRUE)

WRITE THE FINAL DATAFRAME INTO CSV AND SAVE TO LOCAL DRIVE:

# WRITE THE FINAL DATAFRAME INTO CSV AND SAVE TO LOCAL DRIVE:
write.csv(ZOO, "/Users/aysuerdemir/Desktop/R workspace/ERP_Zoo/CrossSectional/Mix/ZOO.csv")
head(ZOO)
##    Subject StimTag Emotion Condition Laterality  MeanAmp_P2  MeanAmp_N2
## 1 AE050318   NegGo     Neg        Go       Left  -8.2741357 -12.4529149
## 2 AE050318   NegGo     Neg        Go    Midline  -8.6567894 -17.6405009
## 3 AE050318   NegGo     Neg        Go      Right -10.9978696 -17.0612959
## 4 AE050318 NegNoGo     Neg      NoGo      Right   3.0738593  -0.6563204
## 5 AE050318 NegNoGo     Neg      NoGo    Midline   0.1368089  -2.7636031
## 6 AE050318 NegNoGo     Neg      NoGo       Left -10.2330683 -11.7855128
##   MeanAmp_P3 Latency_P2 PeakAmp_P2 Latency_N2 PeakAmp_N2 Latency_P3 PeakAmp_P3
## 1   7.006061        200  -6.982536        368 -17.043367        512   9.862223
## 2   4.369997        212  -6.242593        352 -23.058663        612   6.037603
## 3  -1.012820        208  -8.224361        352 -20.626624        576   0.844127
## 4   0.449258        232   5.512485        400  -3.018455        508   3.116096
## 5   3.345500        252   3.300071        452  -5.257907        600   6.301873
## 6   2.221935        188  -8.637317        408 -12.417423        648   5.847591
##   MeanAmp_N2P2 PeakAmp_N2P2 calculator_age_cve calculator_gender_cve race
## 1    -4.178779   -10.060831               38.1                     0    2
## 2    -8.983711   -16.816070               38.1                     0    2
## 3    -6.063426   -12.402263               38.1                     0    2
## 4    -3.730180    -8.530940               38.1                     0    2
## 5    -2.900412    -8.557978               38.1                     0    2
## 6    -1.552445    -3.780106               38.1                     0    2
##   ethnicity calculator_talkergroup_parent tso_calculated
## 1         0                             1            1.9
## 2         0                             1            1.9
## 3         0                             1            1.9
## 4         0                             1            1.9
## 5         0                             1            1.9
## 6         0                             1            1.9
##   disfluency_sldper100words ssi_total disfluency_sldper100words_final
## 1                        12        23                              12
## 2                        12        23                              12
## 3                        12        23                              12
## 4                        12        23                              12
## 5                        12        23                              12
## 6                        12        23                              12
##   talkergroup_final gfta_standard ppvt_standard evt_standard teld_rec_standard
## 1                 1           121           126          123               146
## 2                 1           121           126          123               146
## 3                 1           121           126          123               146
## 4                 1           121           126          123               146
## 5                 1           121           126          123               146
## 6                 1           121           126          123               146
##   teld_exp_standard teld_spokenlang_standard tocs_1_total tocs_2_total
## 1               135                      149           22            6
## 2               135                      149           22            6
## 3               135                      149           22            6
## 4               135                      149           22            6
## 5               135                      149           22            6
## 6               135                      149           22            6
##   tcs_total eprime_condorder_zootask cve_comments comments_tasks handedness_zoo
## 1        25                        1                                         NA
## 2        25                        1                                         NA
## 3        25                        1                                         NA
## 4        25                        1                                         NA
## 5        25                        1                                         NA
## 6        25                        1                                         NA
##   accuracy premature_responses RT_proper RT_all_go TrialNum
## 1 75.83333             9.90099   774.044  706.0297       31
## 2 75.83333             9.90099   774.044  706.0297       31
## 3 75.83333             9.90099   774.044  706.0297       31
## 4 57.50000            23.52941   566.000       NaN       10
## 5 57.50000            23.52941   566.000       NaN       10
## 6 57.50000            23.52941   566.000       NaN       10

REMOVE THESE KIDS BASED ON LOWER ACCURACY + NEGATIVE EEG COMMENTS:

ZOO_bad_subjects <- ZOO %>%
  group_by(Subject) %>%
  filter(all(accuracy[StimTag == "NeutNoGo"] <= 40) |
         all(accuracy[StimTag == "NegNoGo"] <= 40) |
         all(accuracy[StimTag == "NeutGo"] <= 70) |
         all(accuracy[StimTag == "NegGo"] <= 70))

bad_subjects = unique(ZOO_bad_subjects$Subject)
bad_subjects
## [1] "EG030618" "HH061919" "JS121321" "NL041119" "WS051018"
# READ THE COMMENTS FOR THOSE LOW ACCURACY KIDS:
ZOO_filtered <- dplyr::filter(ZOO, Subject %in% bad_subjects)
ZOO_filtered <-  subset(ZOO_filtered, select=c('Subject','talkergroup_final','StimTag','accuracy', 'TrialNum', 'calculator_age_cve','RT_proper', 'cve_comments','comments_tasks','handedness_zoo'))
ZOO_filtered <- ZOO_filtered %>% distinct() # Use distinct function to remove duplicates
print(ZOO_filtered)
##     Subject talkergroup_final  StimTag accuracy TrialNum calculator_age_cve
## 1  EG030618                 1    NegGo 73.94958       26               48.8
## 2  EG030618                 1  NegNoGo 30.00000        6               48.8
## 3  EG030618                 1   NeutGo 84.16667       46               48.8
## 4  EG030618                 1 NeutNoGo 72.50000       13               48.8
## 5  HH061919                 1    NegGo 80.50847       31               76.4
## 6  HH061919                 1  NegNoGo 60.00000       12               76.4
## 7  HH061919                 1   NeutGo 95.00000       52               76.4
## 8  HH061919                 1 NeutNoGo 35.00000       11               76.4
## 9  JS121321                 1    NegGo 69.49153       50               41.5
## 10 JS121321                 1  NegNoGo 85.00000       22               41.5
## 11 JS121321                 1   NeutGo 84.16667       57               41.5
## 12 JS121321                 1 NeutNoGo 72.50000       17               41.5
## 13 NL041119                 1    NegGo 80.83333       45               41.8
## 14 NL041119                 1  NegNoGo 75.00000       16               41.8
## 15 NL041119                 1   NeutGo 62.50000       29               41.8
## 16 NL041119                 1 NeutNoGo 45.00000        9               41.8
## 17 WS051018                 1    NegGo 85.00000       45               77.3
## 18 WS051018                 1  NegNoGo 40.00000        8               77.3
## 19 WS051018                 1   NeutGo 94.95798       46               77.3
## 20 WS051018                 1 NeutNoGo 52.50000       10               77.3
##    RT_proper
## 1   842.5393
## 2   894.8333
## 3   854.0990
## 4   803.7273
## 5   663.9375
## 6   666.4000
## 7   556.8509
## 8   467.3200
## 9   958.7262
## 10 1112.6667
## 11  873.1287
## 12 1088.6250
## 13  920.0206
## 14  693.3000
## 15  844.6267
## 16  891.6818
## 17  537.2451
## 18  412.8571
## 19  563.1316
## 20  491.0000
##                                                                                                                                                                                                                         cve_comments
## 1                                                                                                                                                                          No emotional regulation, no rhythm task, no reaction time
## 2                                                                                                                                                                          No emotional regulation, no rhythm task, no reaction time
## 3                                                                                                                                                                          No emotional regulation, no rhythm task, no reaction time
## 4                                                                                                                                                                          No emotional regulation, no rhythm task, no reaction time
## 5                                                                                                                                                                          Performed TELD B to see if he scored above our cut off.  
## 6                                                                                                                                                                          Performed TELD B to see if he scored above our cut off.  
## 7                                                                                                                                                                          Performed TELD B to see if he scored above our cut off.  
## 8                                                                                                                                                                          Performed TELD B to see if he scored above our cut off.  
## 9                                                                                                                                                                Another speech sample was taken since he was borderline last time. 
## 10                                                                                                                                                               Another speech sample was taken since he was borderline last time. 
## 11                                                                                                                                                               Another speech sample was taken since he was borderline last time. 
## 12                                                                                                                                                               Another speech sample was taken since he was borderline last time. 
## 13                                                                                                                                                                                                                                  
## 14                                                                                                                                                                                                                                  
## 15                                                                                                                                                                                                                                  
## 16                                                                                                                                                                                                                                  
## 17 The child had difficulty attending to most tasks including the NIH toolbox. Fidgeting and changing hands to response frequently. He had hard time sitting still. With the EEg cap he was restless and moving frequently as well. 
## 18 The child had difficulty attending to most tasks including the NIH toolbox. Fidgeting and changing hands to response frequently. He had hard time sitting still. With the EEg cap he was restless and moving frequently as well. 
## 19 The child had difficulty attending to most tasks including the NIH toolbox. Fidgeting and changing hands to response frequently. He had hard time sitting still. With the EEg cap he was restless and moving frequently as well. 
## 20 The child had difficulty attending to most tasks including the NIH toolbox. Fidgeting and changing hands to response frequently. He had hard time sitting still. With the EEg cap he was restless and moving frequently as well. 
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              comments_tasks
## 1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
## 2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
## 3                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
## 4                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
## 5  During the Zoo Task, he required a lot of reminders to not push the button for the helpers. During the second block (affective) of the Zoo Task, he said he was very tired and refused to push the button for about 10-20 trials- required a lot of redirection to keep going. During the Reactivity Task, the second block(unpleasant) he moved a lot and talked a lot- required a lot of redirection to keep going and the skin conductance electrode fell off for part of that block.   RESTING STATE and ZOO TASK are in one file.  
## 6  During the Zoo Task, he required a lot of reminders to not push the button for the helpers. During the second block (affective) of the Zoo Task, he said he was very tired and refused to push the button for about 10-20 trials- required a lot of redirection to keep going. During the Reactivity Task, the second block(unpleasant) he moved a lot and talked a lot- required a lot of redirection to keep going and the skin conductance electrode fell off for part of that block.   RESTING STATE and ZOO TASK are in one file.  
## 7  During the Zoo Task, he required a lot of reminders to not push the button for the helpers. During the second block (affective) of the Zoo Task, he said he was very tired and refused to push the button for about 10-20 trials- required a lot of redirection to keep going. During the Reactivity Task, the second block(unpleasant) he moved a lot and talked a lot- required a lot of redirection to keep going and the skin conductance electrode fell off for part of that block.   RESTING STATE and ZOO TASK are in one file.  
## 8  During the Zoo Task, he required a lot of reminders to not push the button for the helpers. During the second block (affective) of the Zoo Task, he said he was very tired and refused to push the button for about 10-20 trials- required a lot of redirection to keep going. During the Reactivity Task, the second block(unpleasant) he moved a lot and talked a lot- required a lot of redirection to keep going and the skin conductance electrode fell off for part of that block.   RESTING STATE and ZOO TASK are in one file.  
## 9                                                                                                                                                                                                                                                                                                                                                                                                                         It took him a while to get the task during zoo but he got it towards the 2nd quarter. Good attention in general. 
## 10                                                                                                                                                                                                                                                                                                                                                                                                                        It took him a while to get the task during zoo but he got it towards the 2nd quarter. Good attention in general. 
## 11                                                                                                                                                                                                                                                                                                                                                                                                                        It took him a while to get the task during zoo but he got it towards the 2nd quarter. Good attention in general. 
## 12                                                                                                                                                                                                                                                                                                                                                                                                                        It took him a while to get the task during zoo but he got it towards the 2nd quarter. Good attention in general. 
## 13                                                                                                                                                                                                                                                                                                                                                                                              DO NOT use Reactivity data. Child was rarely looking at the screen.  She was talking to mom and putting her head down to rest on the table.
## 14                                                                                                                                                                                                                                                                                                                                                                                              DO NOT use Reactivity data. Child was rarely looking at the screen.  She was talking to mom and putting her head down to rest on the table.
## 15                                                                                                                                                                                                                                                                                                                                                                                              DO NOT use Reactivity data. Child was rarely looking at the screen.  She was talking to mom and putting her head down to rest on the table.
## 16                                                                                                                                                                                                                                                                                                                                                                                              DO NOT use Reactivity data. Child was rarely looking at the screen.  She was talking to mom and putting her head down to rest on the table.
## 17                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
## 18                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
## 19                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
## 20                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
##    handedness_zoo
## 1              NA
## 2              NA
## 3              NA
## 4              NA
## 5               1
## 6               1
## 7               1
## 8               1
## 9               1
## 10              1
## 11              1
## 12              1
## 13              1
## 14              1
## 15              1
## 16              1
## 17             NA
## 18             NA
## 19             NA
## 20             NA
# REMOVE THESE KIDS with <30% accuracy in Nogo, and <60% in Go. 
subjects_to_remove <- c("EG030618","HH061919","WS051018")      

ZOO_good <- ZOO
ZOO_good <- dplyr::filter(ZOO_good, !Subject %in% subjects_to_remove)

write.csv(ZOO_good, "/Users/aysuerdemir/Desktop/R workspace/ERP_Zoo/CrossSectional/Mix/ZOO_good.csv")

DRAW N2P2 and P3 WAVEFORM GRAPHS YOURSELF

# Calculate the average of specific columns (as specificed by these electrode numbers below) across all rows and create a new column with these averages
# Combine all conditions together into a single dataframe
# This step is necessary to reverse the unclass() function we have used above. 
# We need to create subsets from the original combo dataset first ADN THEN unclass before using m.measures()
combo_new <- rbind.data.frame(neg_go, neg_nogo, neut_go, neut_nogo) 
combo_old <- rbind.data.frame(neg_go_old, neg_nogo_old, neut_go_old, neut_nogo_old) 

# Add the "talkergroup_final" column from the "FULL" dataset to the "combo" dataset based on the common "Subject" column
combo_with_group_new <- combo_new %>%
  mutate(talkergroup_final = FULL$talkergroup_final[match(Subject, FULL$Subject)])

combo_with_group_old <- combo_old %>%
  mutate(talkergroup_final = FULL$talkergroup_final[match(Subject, FULL$Subject)])

# REMOVE THESE THREE KIDS FROM THE DATASET:
combo_with_group_new <- dplyr::filter(combo_with_group_new, Subject!="EG030618", Subject!="HH061919", Subject!="WS051018")


# CREATE MEAN N2P2 AND P3 VALUES FOR EACH ROW (TIMEPOINT) USING THE SPECIFIED ELECTRODE NUMBERS 

newnet_rawdata <- combo_with_group_new  %>%
    mutate(
    N2P2_waveform = rowMeans(select(., all_of(FCz_newnets))),
    P3_waveform = rowMeans(select(., all_of(Pz_newnets)))
    )

oldnet_rawdata <- combo_with_group_old  %>%
    mutate(
    N2P2_waveform = rowMeans(select(., all_of(FCz_oldnets))),
    P3_waveform = rowMeans(select(., all_of(Pz_oldnets)))
    )

# Combine old and new net data together:
rawdata <- full_join(newnet_rawdata, oldnet_rawdata)
## Joining with `by = join_by(Subject, Stimulus, Time, V1, V2, V3, V4, V5, V6, V7,
## V8, V9, V10, V11, V12, V13, V14, V15, V16, V17, V18, V19, V20, V21, V22, V23,
## V24, V25, V26, V27, V28, V29, V30, V31, V32, V33, V34, V35, V36, V37, V38, V39,
## V40, V41, V42, V43, V44, V45, V46, V47, V48, V49, V50, V51, V52, V53, V54, V55,
## V56, V57, V58, V59, V60, V61, V62, V63, V64, V65, V66, V67, V68, V69, V70, V71,
## V72, V73, V74, V75, V76, V77, V78, V79, V80, V81, V82, V83, V84, V85, V86, V87,
## V88, V89, V90, V91, V92, V93, V94, V95, V96, V97, V98, V99, V100, V101, V102,
## V103, V104, V105, V106, V107, V108, V109, V110, V111, V112, V113, V114, V115,
## V116, V117, V118, V119, V120, V121, V122, V123, V124, V125, V126, V127, V128,
## V129, talkergroup_final, N2P2_waveform, P3_waveform)`
# How many kids in each group?
talkergroup_counts <- table(rawdata$talkergroup_final)
print(talkergroup_counts)/(275*4)
## 
##     0     1 
## 46200 40700
## 
##  0  1 
## 42 37
# CREATE A SINGLE SUMMARY WAVEFORM FOR N2P2 AND P3 ACROSS ALL PARTICIPANTS IN EACH GROUP
summary_waveforms <-
  rawdata %>%
  group_by(Stimulus, Time, talkergroup_final) %>%
  summarise(Average_N2P2 = mean(N2P2_waveform),Average_P3 = mean(P3_waveform) )
## `summarise()` has grouped output by 'Stimulus', 'Time'. You can override using
## the `.groups` argument.
# Define the X-axis vertical lines
vertical_lines1 <- c(180, 320, 550)  # Specify the X-axis positions
# Define the X-axis vertical lines
vertical_lines2 <- c(400, 750)  # Specify the X-axis positions

#Red
line_colors <- c("NeutGo" = "#D6E4F0", "NeutNoGo" = "#0000CD", "NegGo" = "#FFB6B6", "NegNoGo" = "#FF0000")
# line_colors <- c("NeutGo" = "lightskyblue1", "NeutNoGo" = "mediumblue", "NegGo" = "pink1", "NegNoGo" = "red2")

# DRAW THE GRAPHS
CWNS_N2P2_waveform <- ggplot(data = subset(summary_waveforms, talkergroup_final == 0), 
aes(x = Time, y = Average_N2P2, color = Stimulus)) +
  geom_line(stat = "identity", size = 1.5) +
  labs(x = "Time (ms)", y = "Amplitude in Microvolts (μV)") +
  scale_x_continuous(limits = c(-100, 900), breaks = seq(-100, 900, 100), position = "top") +
  scale_y_continuous(limits = c(-15, 2), breaks = seq(-14, 2, 2)) +
  scale_color_manual(values = line_colors, labels = c("Affective Go", "Affective NoGo","Neutral Go", "Neutral NoGo")) +  # Set line colors manually
  theme(
    axis.text = element_text(size = 14, color = "black"),
    axis.title = element_text(size = 14),
    plot.background = element_rect(fill = "white"),
    axis.line = element_line(linetype = "solid", color = "gray10", size = 1.4),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    legend.text = element_text(size = 12), 
    legend.title =element_blank()
    ) +
  geom_vline(xintercept = vertical_lines1, color = "gray50", linetype = "dashed") +
  geom_hline(yintercept = 0, color = "gray70", linetype = "dotted") +
  geom_vline(xintercept = 0, color = "gray70", linetype = "dotted") +
  ggtitle("CWNS N2P2 Activity") +
  theme(plot.title = element_text(hjust = 0.5, size = 24, face = "bold", vjust = 2)) +
  theme(legend.position = c(.85, .2))  # Set the position of the legend
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
plot(CWNS_N2P2_waveform)
## Warning: Removed 96 rows containing missing values (`geom_line()`).

CWS_N2P2_waveform <- ggplot(data = subset(summary_waveforms, talkergroup_final == 1), 
aes(x = Time, y = Average_N2P2, color = Stimulus)) +
  geom_line(stat = "identity", size = 1.3) +
  labs(x = "Time (ms)", y = "Amplitude in Microvolts (μV)") +
  scale_x_continuous(limits = c(-100, 900), breaks = seq(-100, 900, 100), position = "top") +
  scale_y_continuous(limits = c(-15, 2), breaks = seq(-14, 2, 2)) +
  scale_color_manual(values = line_colors, labels = c("Affective Go", "Affective NoGo","Neutral Go", "Neutral NoGo")) +  # Set line colors manually
  theme(
    axis.text = element_text(size = 14 , color = "black"),
    axis.title = element_text(size = 14),
    plot.background = element_rect(fill = "white"),
    axis.line = element_line(linetype = "solid", color = "gray10", size = 1.4),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    legend.text = element_text(size = 12), 
    legend.title =element_blank()
  ) +
  geom_vline(xintercept = vertical_lines1, color = "gray50", linetype = "dashed") +
  geom_hline(yintercept = 0, color = "gray70", linetype = "dotted") +
  geom_vline(xintercept = 0, color = "gray70", linetype = "dotted") +
  ggtitle("CWS N2P2 Activity") +
  theme(plot.title = element_text(hjust = 0.5, size = 24, face = "bold", vjust = 2)) +
  theme(legend.position = c(.85, .2))  # Set the position of the legend
plot(CWS_N2P2_waveform)  
## Warning: Removed 108 rows containing missing values (`geom_line()`).

CWNS_P3_waveform <- ggplot(data = subset(summary_waveforms, talkergroup_final == 0), 
aes(x = Time, y = Average_P3, color = Stimulus)) +
  geom_line(stat = "identity", size = 1.3) +
  labs(x = "Time (ms)", y = "Amplitude in Microvolts (μV)") +
  scale_x_continuous(limits = c(-100, 900), breaks = seq(-100, 900, 100), position = "top")+
  scale_y_continuous(limits = c(-2, 13), breaks = seq(-2, 13, 2)) +
  scale_color_manual(values = line_colors, labels = c("Affective Go", "Affective NoGo","Neutral Go", "Neutral NoGo")) +  # Set line colors manually
  theme(
    axis.text = element_text(size = 14, color = "black"),
    axis.title = element_text(size = 14),
    plot.background = element_rect(fill = "white"),
    axis.line = element_line(linetype = "solid", color = "gray10", size = 1.4),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    legend.text = element_text(size = 12), 
    legend.title =element_blank()
  ) +
  geom_vline(xintercept = vertical_lines2, color = "gray50", linetype = "dashed") +
  geom_hline(yintercept = 0, color = "gray70", linetype = "dotted") +
  geom_vline(xintercept = 0, color = "gray70", linetype = "dotted") +
  ggtitle("CWNS P3 Activity") +
  theme(plot.title = element_text(hjust = 0.5, size = 24, face = "bold", vjust = 2)) +
    theme(legend.position = c(.65, .3))
plot(CWNS_P3_waveform)
## Warning: Removed 96 rows containing missing values (`geom_line()`).

CWS_P3_waveform <- ggplot(data = subset(summary_waveforms, talkergroup_final == 1),
aes(x = Time, y = Average_P3, color = Stimulus)) +
  geom_line(stat = "identity", size = 1.3) +
  labs(x = "Time (ms)", y = "Amplitude in Microvolts (μV)") +
  scale_x_continuous(limits = c(-100, 900), breaks = seq(-100, 900, 100), position = "top")+
  scale_y_continuous(limits = c(-2, 13), breaks = seq(-2, 13, 2)) +
  scale_color_manual(values = line_colors, labels = c("Affective Go", "Affective NoGo","Neutral Go", "Neutral NoGo")) +  # Set line colors manually
  theme(
    axis.text = element_text(size = 14 , color = "black"),
    axis.title = element_text(size = 14),
    plot.background = element_rect(fill = "white"),
    axis.line = element_line(linetype = "solid", color = "gray10", size = 1.4),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    legend.text = element_text(size = 12), 
    legend.title =element_blank()
  ) +
  geom_vline(xintercept = vertical_lines2, color = "gray50", linetype = "dashed") +
  geom_hline(yintercept = 0, color = "gray70", linetype = "dotted") +
  geom_vline(xintercept = 0, color = "gray70", linetype = "dotted") +
  ggtitle("CWN P3 Activity") +
  theme(plot.title = element_text(hjust = 0.5, size = 24, face = "bold", vjust = 2)) +
  theme(legend.position = c(.65, .3))
plot(CWS_P3_waveform)  
## Warning: Removed 96 rows containing missing values (`geom_line()`).